Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django : Generic Foreign key dumpdata: Can't resolve dependencies

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.

like image 625
Babu Avatar asked Dec 24 '12 12:12

Babu


1 Answers

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")
like image 59
mgibsonbr Avatar answered Sep 30 '22 18:09

mgibsonbr