Answer the question
In order to leave comments, you need to log in
How can django store the pk/id of an object in modelchoicefield?
In model.py, something like this model
class OrderItem(models.Model):
description = models.CharField(
blank=True,
null=True,
max_length=255)
car_id = models.PositiveIntegerField(blank=True,
null=True,)
class OrderItemForm(forms.ModelForm):
# class Meta:
# model = OrderItem
# car_id = forms.ModelChoiceField(queryset=Car.objects.filter(pk=self.car_id))
def __init__(self, *args, **kwargs):
super(OrderItemForm, self).__init__(*args, **kwargs)
self.fields['car_id'] = forms.ModelChoiceField(queryset=Car.objects.all())
class OrderItemAdmin(admin.TabularInline):
# raw_id_fields = ("id",)
form = OrderItemForm
model = OrderItem
exclude = ('hash', 'locked','options_hash', 'description2')
# fields = ("description", "price", "quantity", 'car_id')
def __init__(self, *args, **kwargs):
super(OrderItemAdmin, self).__init__(*args, **kwargs)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question