Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to limit ForeignKey in django
    primarykey
    data
    text
    <p>i am using django 1.2 i have two models.</p> <p><strong>EDIT: i just found a better example:</strong></p> <pre><code>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) </code></pre> <p>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.</p> <p><strong>ORIGINAL EXAMPLE:</strong></p> <pre><code>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') </code></pre> <p>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.<br> for example if i have version named '1.1' that has platform joomla and version '1.2' that has wordpress as platform.<br> so when i will choose default_version dropdown in the admin for wordpress i want it to <strong>only show</strong> me version '1.2' in the drop down. now it shows me all of the versions.</p> <p>i am trying to limit limit_choices_to as shown <a href="https://code.djangoproject.com/ticket/3096" rel="nofollow">here</a> so i try this:</p> <pre><code>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') </code></pre> <p>but i am lost as to what to put insted of the XXXX i try putting self but it did not work.<br> i have also tried </p> <pre><code>limit_choices_to={'platform.name':name} </code></pre> <p>i did not work either.</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload