Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

form_ajax_refs to model with primary key attribute not matching the column in the DB #1072

Closed
vstoykov opened this issue Oct 6, 2015 · 1 comment

Comments

@vstoykov
Copy link

vstoykov commented Oct 6, 2015

When I have the following models:

class Country(db.Model):

    name = db.Column(db.Text)
    alpha2 = db.Column('iso3166alpha2', db.Text)
    alpha3 = db.Column('iso3166alpha3', db.Text)
    numeric = db.Column('iso3166numeric', db.Integer, primary_key=True, autoincrement=False)


class Customer(db.Model):
    id = db.Column('customer_id', db.Integer, primary_key=True, nullable=False)
    name = db.Column(db.Text)
    country_number = db.Column('country', db.Integer, db.ForeignKey('country.iso3166numeric'), nullable=False)
    country = db.relationship('Country', foreign_keys=country_number)

And I try to register Customer model in administration like that

class CustomerModelView(StaffModelView):
    form_ajax_refs = {
        'country': {
            'fields': ['alpha3', 'name'],
            'page_size': 10
        }
    }

I receive an error:

AttributeError: 'Country' object has no attribute 'iso3166numeric'

The problem is obviously that attribute name of the primary key of the Country model does not match the name of the column. But it is supported by the SQLAlchemy.

The problem occurs in QueryAjaxModelLoader.format method https://github.com/flask-admin/flask-admin/blob/v1.3.0/flask_admin/contrib/sqla/ajax.py#L50-L54

I found that in SQLAlchemy there is a way to get the value of primary key by using primary_key_from_instance method of the Mapper

I will try to provide a pull request to show what I mean.

@vstoykov
Copy link
Author

vstoykov commented Oct 6, 2015

My PR #1073 has changes for fixing this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant