سؤال

I'm trying to save an existing instance of a customer record. Its model has a M2M to the vehicle model (since a customer can multiple vehicles). After reading several questions/answer here, I still do not know how to solve this.

Customer model:

class Customer(models.Model):
    vehicle_id = models.ManyToManyField(VehicleSale)
    name = models.CharField(max_length=40, blank=True, db_index=True, null=True,
                                     verbose_name='name')
    lic = models.CharField(max_length=20, blank=True, db_index=True, null=True,
                                verbose_name='license')
    addr = models.CharField(max_length=40, blank=True, null=True, verbose_name='address')
    city = models.CharField(max_length=15, blank=True, null=True, verbose_name='city')
    state = models.CharField(max_length=2, blank=True, null=True, verbose_name='state')
    zip = models.CharField(max_length=10, blank=True, null=True, verbose_name='zipcode')
    email = models.EmailField(blank=True, null=True, verbose_name='email')
    tel1 = models.CharField(max_length=15, blank=True, verbose_name='Tel. 1', null=True)
    tel2 = models.CharField(max_length=15, blank=True, verbose_name='Tel. 2', null=True)
    ssn = models.CharField(max_length=12, blank=True, db_index=True, null=True,verbose_name='SSN')

    class Meta:
        db_table = 'customer'

    def __unicode__(self):
        return self.name

    def save(self, *args, **kwargs):
        self.name = self.name.upper()
        self.addr = self.addr.upper()
        self.city = self.city.upper()
        self.state = self.state.upper()

        return super(Customer, self).save(*args, **kwargs)

In the view, after defining customer as

customer = current_vehicle.customer_set.all()

I tried the following:

if 'customer' in request.POST:
    if customer:
        customer_form = CustomerForm(request.POST, instance=customer[0])
        if customer_form.is_valid():
            customer_form.save()

Also tried adding before customer_form is defined:

customer.vehicle_id = current_vehicle.id

And then this after the form:

customer_form.vehicle_id = current_vehicle.id

Form is not valid so it's not saved. Upon checking {{ form.errors}}, it always reports vehicle_id is required.

Finally, after the answer in this, I adjusted it to my scenario by adding:

obj = customer_form.save(commit=False)

and hoping to assign vehicle_id, but it fails immediately.

What am I missing?

Thanks.

1st EDIT: The section on the view now looks as:

customer_form = CustomerForm(request.POST, instance=customer[0])
customer_form.save()
customer_form.vehicle_id.add(current_vehicle)
هل كانت مفيدة؟

المحلول

You are misunderstanding what a ManyToMany field is here:

customer_form.vehicle_id = current_vehicle.id

vehicle_id is defined as a ManyToMany field on your Customer model, therefore you can't just assign a single id to it. You have to add an instance of VehicleSale model, eg:

customer_form.vehicle_id.add(current_vehicle)

See docs here:
https://docs.djangoproject.com/en/dev/topics/db/examples/many_to_many/

See also this answer for why you can't save until you populate the vehicle_id relation:
https://stackoverflow.com/a/2529875/202168

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top