[Answered ]-Nested foreignkey query

0πŸ‘

βœ…

Ok, the info found here:

http://bitkickers.blogspot.dk/2010/04/django-manytomany-error-cannot-resolve.html

indicated that load order of the model could yield this kind og error, so i changed the models.py file from this order:

class Image(models.Model):
    folders = models.ManyToManyField('Folder')
    title = models.CharField()

class Folder(models.Model):
    user = models.ForeignKey(User)
    name = models.CharField()

class CanAccess(models.Model):
    folder = models.ForeignKey(Folder)
    user = models.ForeignKey(User)

to this order:

class CanAccess(models.Model):
    folder = models.ForeignKey("Folder")
    user = models.ForeignKey(User)

class Image(models.Model):
    folders = models.ManyToManyField('Folder')
    title = models.CharField()

class Folder(models.Model):
    user = models.ForeignKey(User)
    name = models.CharField()

And that actually seems to fix the problem.

πŸ‘€Drewes

2πŸ‘

You don’t need the _set when doing a lookup:

Image.objects.filter(folders__canaccess__user=current_user)

Leave a comment