Django limit_choices_to - ForeignKey

1,296 views
Skip to first unread message

Ganea Ionut

unread,
Jul 23, 2014, 6:00:57 AM7/23/14
to django...@googlegroups.com
Hello there,

Recently i've been tasked with using the 'limit_choices_to' attribute of a ForeignKey field, in order to limit choices.

My models are like this (params -> max_length, choices, etc):

class DeviceFeatures(models.Model):

# declare choices here: feature_choices #

features = models.CharField(params)
namespace = models.CharField(params)

class Device(models.Model):

jid = models.CharField(params)
name = models.CharField(params)
features = models.ManyToManyField(DeviceFeatures)

class DeviceMember(models.Model):

device = models.ForeignKey(Device)
features = models.ForeignKey(DeviceFeatures, limit_choices_to={"feature__in": [str(feature) for feature in device.features.all()]}) (theory)

What i would like to know, is how i can access the features of a device, inside of the DeviceMember model. More exactly, i want only the features of the device that DeviceMember - ForeignKey attribute points out to.

I've been studying for some hours how limit_choices_to works...and i know this is how i'm supposed to use it, i just can't figure out how to access the features of the foreign key device.

thank you all.

cmawe...@gmail.com

unread,
Jul 31, 2014, 12:24:26 PM7/31/14
to django...@googlegroups.com
If you are using the admin:


Otherwise, you'll need to dynamically create that field or the entire form.
Reply all
Reply to author
Forward
0 new messages