0

i am using django 1.2 i have two models.

EDIT: i just found a better example:

class Parent(models.Model):
    name = models.CharField(max_length=255)
    favorite_child = models.ForeignKey(Child)

class Child(models.Model):
    name = models.CharField(max_length=255)
    myparent = models.ForeignKey(Parent)

in this example i would like to be able to choose a favorite child but the problem is that the admin will give me all the children to choose from and not just the ones who are the children of the parent i am currently editing.

ORIGINAL EXAMPLE:

class Version(models.Model):
    name = models.CharField(max_length = 255)
    platform = models.ForeignKey("Platform",related_name='version_platform')

class Platform(models.Model):
    name = models.CharField(max_length = 255)
    default_version = models.ForeignKey(Version,related_name='platform_default_version')

i want the django admin to limit the drop down when i choose default_version so that i will only be able to choose those versions that have the current platform.
for example if i have version named '1.1' that has platform joomla and version '1.2' that has wordpress as platform.
so when i will choose default_version dropdown in the admin for wordpress i want it to only show me version '1.2' in the drop down. now it shows me all of the versions.

i am trying to limit limit_choices_to as shown here so i try this:

class Platform(models.Model):
    name = models.CharField(max_length = 255)
    default_version = models.ForeignKey(Version,limit_choices_to={'platform':XXXXX},related_name='platform_default_version')

but i am lost as to what to put insted of the XXXX i try putting self but it did not work.
i have also tried

limit_choices_to={'platform.name':name}

i did not work either.

in this example i would like to be able to choose a favorite child but the problem is that the admin will give me all the children to choose from and not just the ones who are the children of the parent i am currently editing.

yossi
  • 12,945
  • 28
  • 84
  • 110
  • 1
    limit_choices_to doesn't work with dynamic values, see http://stackoverflow.com/questions/1968596/django-limit-choices-to-is-this-correct i'm not sure if that will help your case as i'm guessing this is on adding a new Platform not editing? – JamesO Mar 08 '12 at 15:08
  • Regarding your second example, you'd rather have a `favorite` boolean field for your child, and then you'd use `unique_together`. – Thomas Orozco Mar 08 '12 at 15:38
  • but then i will be able to choose 2 favorite children which open room for mistakes. – yossi Mar 08 '12 at 15:43

3 Answers3

2

You could try formfield_for_foreignkey:

class PlatformAdmin(admin.ModelAdmin):
    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == "default_version":
            kwargs["queryset"] = Version.objects.filter(platform=self.instance.pk)
        return super(PlatformAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
dan-klasson
  • 13,734
  • 14
  • 63
  • 101
2

i found a solution based on Django: accessing the model instance from within ModelAdmin?

adding to the admin.py solution:

class ParentForm(ModelForm):
def __init__(self, *args, **kwargs):
    super(ParentForm, self).__init__(*args, **kwargs)
    self.fields['favorite_child'].queryset = \
    Child.objects.filter(parent=self.instance.pk)

class ParentAdmin(admin.ModelAdmin):
    form = ParentForm
Community
  • 1
  • 1
yossi
  • 12,945
  • 28
  • 84
  • 110
0

The issue here has gotten skewed. When you're talking about the version options adapting based on which platform is chosen, you're talking about AJAX. All you need to do is create a view that accepts a platform as an argument and returns a JSON-formatted list of versions.

Then, you attach a hander to the onchange event of the platform select box that will fetch that view via AJAX and use the JSON response to construct a new set of options for the version select box. Replace the old options with the new and your done.

There's plenty of examples on this site and on the web at large.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • i am talking about the django admin, if i would have done the view myself i would not had this problem,btw even without using client side. – yossi Mar 08 '12 at 16:17
  • I understood it as he wants to limit the values in the Version drop down menu on the Platform's change_view page. – dan-klasson Mar 08 '12 at 16:35
  • The problem is limiting the version drop down in Django itself is going to cause problems if the platform is ever changed. If you switch the platform, you'll still only have choices for the old platform, and worse, even if you implement AJAX to update the version choices, it won't validate because it's limited to just those own by the old platform. You *need* to pass the whole potential list of versions unaltered and then filter with AJAX to cover all usage scenarios. @yossi: the fact that this is in the admin has zero bearing on the ability to use AJAX to change the choices. – Chris Pratt Mar 08 '12 at 16:55