pythondjangodjango-modelsdjango-contenttypes

How to access a ForeignKey Object's attribute on a dynamic basis


I'm currently trying to access the ForeignKey to attribute in a for loop as I require it to be dynamic. Neither django's docs nor online search delivered any helpful results. Here's what I'm using: Django 1.11 with Django CMS 3.5.2 and Django Countries package. The error message is:

AttributeError: 'ForeignKey' object has no attribute 'to

However, accessing the field's name or verbose_name or even choices attribute (for charFields or IntegerFields) works.

models.py

company = models.ForeignKey(verbose_name=_('Company'), blank=False, null=False, to='accounts.CompanyName',
                            on_delete=models.CASCADE)

views.py

def generate_view(instance):
    model = apps.get_model(app_label='travelling', model_name=str(instance.model))
    data = dict()
    field_list = eval(instance.fields)
    fields = model._meta.get_fields()
    output_list = list()

    for field in fields:
        for list_field in field_list:
            if field.name == list_field:
                options = list()
                if field.__class__.__name__ == 'ForeignKey':
                    print(field.to) # Here's the error
                elif field.__class__.__name__ == 'CountryField':
                    for k, v in COUNTRIES.items():
                        options.append((k, v)) # Works properly
                elif field.__class__.__name__ == 'ManyToManyField':
                    pass # Yields the same issues as with foreign keys

                output_list.append({
                    'name': field.name,
                    'verbose': field.verbose_name,
                    'options': options,
                    'type': field.__class__.__name__
                })

    return data

Solution

  • As you see, there isn't an attribute called to. That's the name of a parameter of the ForeignKey initializer. Since the argument can be a string model reference, or "self", it makes sense that the attribute representing the actual model target should have a different name.

    The Field attribute reference defines the API for introspecting field objects. What you're after is something like:

    if field.many_to_one:
        print(field.related_model)