I
I
Ilya2016-02-15 09:10:27
Django
Ilya, 2016-02-15 09:10:27

How to pass object to foreign?

I want to transfer an object to the foreignkey field so as not to draw the field on the form. That is, we send the id of the object, and this object is written to the database. Tell me how to do this, I did not find anything to work with this type of fields

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ilya, 2016-02-26
@nuBacuk

Made it like this.

form_valid = form.save(commit=False)
order_start = Order.objects.get(slug=slug)
form_valid.order = order_start
form_valid.save()

Z
zigen, 2016-02-15
@zigen

class Employee(models.Model):
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    type = models.ForeignKey('EmployeeType')
Class EmployeeType(models.Model):
    type = models.CharField(max_length=100)

Code:
employee = get_object_or_404(Employee, pk=1)
type = get_object_or_404(EmployeeType, pk=2)
employee.type = type

djbook.ru/rel1.8/ref/models/relations.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question