mjr mjr - 5 months ago 9
Python Question

Django- limit_choices_to using 2 different tables

I fear that what I am trying to do might be impossible but here we go:

Among my models, I have the following

Class ParentCategory(models.Model):
name = models.CharField(max_length=128)

def __unicode__(self):
return self.name
Class Category(models.Model):
parentCategory = models.ForeignKey(ParentCategory, on_delete=models.CASCADE, )
name = models.CharField(max_length=128)

def __unicode__(self):
return self.name

Class Achievement(models.Model):
milestone = models.ForeignKey(Milestone, on_delete=models.CASCADE)
description = models.TextField( )
level_number = models.IntegerField()
completeion_method = models.ForeignKey(Category, on_delete = models.CASCADE, limit_choices_to={'parentCategory.name':'comp method'})

def __unicode__(self): # TODO:
return description[0,75] + '...'


I know the completion method field throws an error because it is not correct syntax. But is there a way to achieve the wanted result using a similar method?

Answer

Maybe this will work:

limit_choices_to={'parentCategory__name': 'comp method'}