I've two model. I would like to save data from ForeignKey model. I'm 
created a modelform and save with my main foreignkey model. But I got this 
error ValueError at /c/customer/1/ Cannot assign "'1'": 
"BillingData.customer" must be a "CustomerData" instance.

I created Django model form and hocked up with view.

models.py file

class CustomerData(models.Model):
    customer_name = models.CharField(max_length=100)
    customer_no = models.CharField(max_length=100, default='', blank=True)
    mobile_number = models.IntegerField()
    alternative_phone = models.IntegerField(null=True, blank=True)
    union_name = models.ForeignKey(UnionName, on_delete=models.SET_NULL, 
null=True)
    word_name = models.ForeignKey(UnionWordName, on_delete=models.SET_NULL, 
null=True)
    full_address = models.CharField(max_length=200, null=True, blank=True)
    create_date = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    def __str__(self):
        return '%s, Mobile: %s' % (self.customer_name, self.mobile_number)

    def get_absolute_url(self):
        return reverse('customer_data', kwargs={'pk': self.pk})

class BillingData(models.Model):
    bill_no = models.CharField(max_length=100, default='', blank=True)

    customer = models.ForeignKey(CustomerData, on_delete=models.CASCADE)
    sales_person = models.ForeignKey(settings.AUTH_USER_MODEL, 
on_delete=models.SET_NULL, null=True)
    customer_money = models.IntegerField()
    create_date = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    def __str__(self):
        return '%s %s' % (self.customer.customer_name, self.create_date.date())

    def get_absolute_url(self):
        return reverse('customers.views.BillingPage', kwargs={'pk': self.pk})

forms.py file 

class BillCreateForms(forms.ModelForm):
    bill_no = forms.CharField(max_length=100)
    customer = forms.ChoiceField(choices=[(x.id, x.customer_name) for x in 
CustomerData.objects.all()])
    customer_money = forms.IntegerField()

    def save(self, commit=True):
        instance = super(BillCreateForms, self).save(commit=False)
        customer_pk = self.cleaned_data['customer']

        instance.customer_id = CustomerData.objects.get(pk=customer_pk).pk
        instance.save(commit)
        return instance

    class Meta:
        model = BillingData
        fields = ('bill_no', 'customer', 'customer_money',)

views.py file

class CustomerDataView(FormMixin, generic.DetailView):
    model = CustomerData
    form_class = BillCreateForms
    template_name = "customers/customerdata_detail.html"
    print(form_class)

    success_url = '/c/'

    def post(self, request, *args, **kwargs):
        if not request.user.is_authenticated:
            return HttpResponseForbidden()
        form = self.get_form()
        if form.is_valid():
            return self.form_valid(form)
        else:
            return self.form_invalid(form)

I expect data save with foreignkey relation data. But doesn't save here.


models.py file https://paste.ubuntu.com/p/8RKDGwjhgh/ 
<https://paste.ubuntu.com/p/8RKDGwjhgh/?fbclid=IwAR0gO5OflCkVDFWAs9jXc3yPhNmCMn_4ZEGcr-nvL9QAZxFsgNhRG7E4YlA>
forms.py file https://paste.ubuntu.com/p/gD4B5nFWJP/ 
<https://paste.ubuntu.com/p/gD4B5nFWJP/?fbclid=IwAR2x0m3NoyLIYDeSug_xtZxPG4ZGItfp3ICzSxMozwSLOxnrt1TBVce-A0I>
views.py file https://paste.ubuntu.com/p/njyfwNztY9/ 
<https://paste.ubuntu.com/p/njyfwNztY9/?fbclid=IwAR2ZDD8Xgit0VPxm4W4qvaPUFyQsSdeNR77oqQSdWTRw9c9b5PdMU8JTKvI>


this is full traceback paste.ubuntu.com/p/Z92rVxJpnJ

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/328b139a-7c22-4585-8768-e8ee963435e7%40googlegroups.com.

Reply via email to