Reputation: 10740
Django 1.3.
Suppose I have models:
class Inspector(models.Model):
field = models.IntegerField()
Model_1(models.Model):
inspectors = ManyToManyField(InspectorData)
And I have to create another model, where a field can store data only from Model_1().inspectors
attribute. Smth like:
class Model_2(models.Model):
model_1_inspectors = ManyToManyField(Model_1__inspectors)
How can I accomplish this in Django (if it is possible)?
Thanks in advance.
Upvotes: 0
Views: 56
Reputation: 22561
First option is to override save
method in Model_1
and on every save operation update inspectors in specially created model Model1inspectors
. This way `Model_2' will look just like yours from question.
But i think it's better to customize queryset for that ManyToManyField
in your form like this:
class Model_2Form(forms.ModelForm):
class Meta:
model = Model_2
def __init__(self, *args, **kwargs):
super(Model_2Form, self).__init__(*args, **kwargs)
self.fields['model_1_inspectors'].queryset = InspectorData.objects.filter(
pk__in=Model_1.objects.values('inspectors').distinct())
Upvotes: 1
Reputation: 11070
import the model required as
from myapp_folder.models import Inspector
class Model_2(models.Model):
model_1_inspectors = ManyToManyField(Inspector)
This will work
Upvotes: 0