Django : Generic Foreign key dumpdata: Can't resolve dependencies
Asked Answered
D

2

5

I use Generic Foreign keys to relate different profiles with my Users model which is inherited from auth.User. I'm not able to do dumpdata though with the --natural option passed. It says,

Error: Can't resolve dependencies for myproject.AdminProfile, myproject.TeacherProfile, myproject.Users in serialized app list.

According to documentation, it's said that we need to implement natural_key methods to take and flash fixtures which involves generic relations. How could I do that with my models presented here?

class Users(User):
    location = models.TextField('Location', blank=True)
    created_by = models.ForeignKey('self', null=True, blank=True, related_name='created_by_user')

    # Generic foreign key setup to hold the extra attributes
    profile_contenttype = models.ForeignKey(ContentType, null=True, blank=True)
    profile_object_id = models.PositiveIntegerField('Extra ID', null=True, blank=True)
    profile_object = generic.GenericForeignKey('profile_contenttype', 'profile_object_id')


class AdminProfile(models.Model):
    organization = models.CharField('Organization', max_length=100)

    # profile reverse relation to get the user
    users_link = generic.GenericRelation('Users', content_type_field='profile_contenttype',
                                         object_id_field='profile_object_id')

class TeacherProfile(models.Model):
    designation = models.CharField('Designation', max_length=100)

    # profile reverse to get the user
    users_link = generic.GenericRelation('Users', content_type_field='profile_contenttype',
                                         object_id_field='profile_object_id')

Using Django 1.4.3 and Postrgres.

Down answered 24/12, 2012 at 12:54 Comment(0)
W
8

Your issue seems unrelated to the absence of natural key methods. I tested your [original] code as-is on Django 1.4 and 1.2.5 using SQLite and was able to dump data with natural keys with no errors.

After some searching, I found that this issue appears when there are cyclic dependencies between models (including models with self-references). As your updated code shows, there's a self reference in the Users model, so that's where the problem is. This bug was introduced in Django 1.3 and, despite being already fixed, it's still not available AFAIK in the stable versions (tested up to 1.4.3). In the beta version (1.5b2), however, your code works fine.

If using a beta version (or downgrading to 1.2) is not an option, then your only solution might be creating another model indeed. Something like:

class CreatedBy(models.Model):
    creator = models.ForeignKey(Users, related_name="created_by_user")
    created = models.ForeignKey(Users, unique=True, related_name="created_by")
Wallace answered 27/12, 2012 at 4:9 Comment(3)
I found that this issue appears when there are cyclic dependencies between models (including models with self-references). Thanks for the info. Actually I had a self-reference field. Sorry for not adding that first time(check the edit now). When I remove it I'm able to get the dump. But I need that field anyway. Is that possible? Or I have to create an another model to keep that ralation?Down
@Down That's it then! Check my updated answer. Your code works fine in 1.2.5 and 1.5b2 (where this bug has already been fixed), but not in 1.4 (and I presume neither in 1.3).Wallace
Thanks for your detailed answer and multi-version test results. :) I'll go with creating an another model then. It's very weird and pity that it's not fixed in 1.4.Down
O
0

The more general problem (circular dependencies involving natural keys, instead of just self-references with natural keys) is an open bug and reported here: https://code.djangoproject.com/ticket/31051

Orison answered 1/12, 2019 at 23:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.