A
A
Alexander Alexandrov2015-09-01 09:48:37
Django
Alexander Alexandrov, 2015-09-01 09:48:37

How can django store the pk/id of an object in modelchoicefield?

58d0c7b8d9fb465690a91b390306abfc.pngIn 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,)

admin.py
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())

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)

Where and what event do I need to override? tried save, save_model, save_related, but validation on integer happens somewhere earlier. Please help me figure out where, thanks!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question