In our application, we have model A
that has fields for a generic relation, because it could be related to an instance of either one of two models ( B
or C
).
When populating a non-generic ForeignKey field, we can use the ModelChoiceField. Our problem is we cannot find a MultipleModelsChoiceField, whose choices would be populated from querysets on both B
and C
.
How would one go about solving this problem by reusing as much as possible existing Django code ?
You can do something like this:
class YourForm(forms.ModelForm):
your_field = forms.ChoiceField()
def __init__(self, *args, **kwargs):
super(YourForm, self).__init__(*args, **kwargs)
your_generic_relations_objects = list(FirtsModel.object.all()) + list(SecondModel.objects.all()) # and etc
object_choices = []
for obj in your_generic_relations_objects:
type_id = ContentType.objects.get_for_model(obj.__class__).id
obj_id = obj.id
form_value = 'type:%s-id:%s' % (type_id, obj_id)
display_text = str(obj)
object_choices.append([form_value, display_text])
self.fields['your_field'] = forms.ChoiceField(choices=object_choices)
class Meta:
model = YourModel
fields = [
'your_field' # and others
]
def save(self, *args, **kwargs):
object_string = self.cleaned_data['your_field']
matches = re.match("type:(\d+)-id:(\d+)", object_string).groups()
content_type_id = matches[0]
object_id = matches[1]
content_type = ContentType.objects.get(id=content_type_id)
self.instance.object_id = object_id
self.instance.content_type = content_type
return super(YourForm, self).save()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.