Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

related_name argument not working as expected in Django model?

Tags:

python

django

I recently got a ForeignKey clash in my Django model. I have the need to have two foreign keys (owner, assigned_to) ultimately pointing to the same model (a user).

From what I understand I need a related_name argument to solve that problem. So I did that:

assigned_to = models.ForeignKey(TaskUser, blank=True, null=True, related_name='user_assignment') 

and

owner = models.ForeignKey(TaskUser, related_name="user_ownership" 

But I'm still getting an error:

tasks.task: Accessor for field 'owner' clashes with related field 'TaskUser.user _ownership'. Add a related_name argument to the definition for 'owner'. tasks.task: Reverse query name for field 'owner' clashes with related field 'TaskUser.user_ownership'. Add a related_name argument to the definition for 'owner'. 

Why am I still getting this error?

There is one catch, owner is in a super class (BaseWidget) and assigned_to is in a sub class (Task). Are there issues with using related_name in an inheritance relationship? Do I need to just override the inheritance of owner and redefine related_name in the sub class instead? I'd appreciate any help!

like image 919
Mark Nenadov Avatar asked Apr 10 '11 11:04

Mark Nenadov


People also ask

What does Related_name mean in Django?

The related_name attribute specifies the name of the reverse relation from the User model back to your model. If you don't specify a related_name, Django automatically creates one using the name of your model with the suffix _set.

What is Onetoone field in Django?

This field can be useful as a primary key of an object if that object extends another object in some way. For example – a model Car has one-to-one relationship with a model Vehicle, i.e. a car is a vehicle. One-to-one relations are defined using OneToOneField field of django.

What is Foreignkey Django models?

Introduction to Django Foreign Key. A foreign key is a process through which the fields of one table can be used in another table flexibly. So, two different tables can be easily linked by means of the foreign key. This linking of the two tables can be easily achieved by means of foreign key processes.

What is abstract true in Django model?

An abstract model is a base class in which you define fields you want to include in all child models. Django doesn't create any database table for abstract models. A database table is created for each child model, including the fields inherited from the abstract class and the ones defined in the child model.


1 Answers

If you have ForeignKey relationships in an abstract base class every class inheriting from it will have this relationship. As a result of this you must not 'hardcode' its related_name, because all sub classes will try to create the same accessor on the realted class (TaskUser in this case).

You should better do something like:

owner = models.ForeignKey(TaskUser, related_name="%(app_label)s_%(class)s_ownership") 

See the django docs on this.

like image 118
Bernhard Vallant Avatar answered Sep 20 '22 18:09

Bernhard Vallant