Guys I need quick help.

There are two models and I would like to store a column field from one
model to another model.

models.py:

class Company(models.Model):
   name = ...
 def __str__(self):
         return self.name
class Rate(models.Model):
   company = models.ForeignKey(Company)
   currency = ...
   name = ...

 def __str__(self):
         return self.name + " " + self.currency
class Client(models.Model):
   name = ...
   currency = ....
   company = models.ForeignKey(Company)
   base_rate = models.ForeignKey(Rate)

 def __str__(self):
         return self.name

forms.py:

class ClientForm(forms.ModelForm):
    class Meta:
        model = Client
        fields = (
               "name",
               "company",
               "base_rate", )

views.py:

class ClientCreateView(FormView):
    template_name = "client/new_package.html"
    form_class = ClientForm
    success_url = reverse_lazy("home")

    def form_valid(self, form):
        detail = form.save(commit=False)
        base_rate_id = form.cleaned_data['base_rate']
        detail.currency = Rate.objects.values_list("currency",
flat=True).filter(base_rate_id=base_rate_id)
        detail.save()

        if detail is not None:
            return redirect('display_package' , detail.id)
        else:
            return super(ClientCreateView, self).form_valid(form)

Basically, I want selected currency value to be saved in my client model
from Rate model. Any help will be appreciated as from cleaned data I am
getting *str* returned value instead of selected option id value (i.e. *str*
from client model ).


StackOverflow Link:
https://stackoverflow.com/questions/51947018/django-modelform-foreign-key-dropdown-passes-str-but-stores-fkey-id

Regards and Thanks,
Himanshu | hmn...@gmail.com

-- 
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAJz1rM%2BsK6XN_xJVfqbXAos4WgAVOW9VLy-WZjCT3XFNuP%3DZkg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to