How to select the proper column when I use ForeignKey?

25 views
Skip to first unread message

Kubilay Yazoğlu

unread,
Nov 26, 2017, 5:49:15 PM11/26/17
to Django users
Hello. I have two models. In one of them, I declared a foreign key to the other one. Since this is done in Django by only specifying the Class name, without specifying the field name(column name), when I try to create an object in admin panel, it pulls the wrong column information.

Two models A and B.
B has a Foreignkey for accessing A.
A has two fields Field1 and Field2.
When I try to create an instance of B, in the field of Foreignkey, there is a drop down menu but the content is not from the right field of A.

How to specify it?

Matemática A3K

unread,
Nov 27, 2017, 12:55:45 AM11/27/17
to django...@googlegroups.com
What Django shows in that dropdown is the __str__() method of the object, define it to your needs in A :)

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django...@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/ad0d40a3-cd7f-4f2f-821d-31cc007657b1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kubilay Yazoğlu

unread,
Nov 27, 2017, 8:50:56 AM11/27/17
to Django users
Thanks for your answer. I have one more question. What if there are more than one primary keys in that table? I'm talking about the unique_together case.

27 Kasım 2017 Pazartesi 08:55:45 UTC+3 tarihinde Matemática A3K yazdı:
What Django shows in that dropdown is the __str__() method of the object, define it to your needs in A :)
On Sun, Nov 26, 2017 at 7:49 PM, Kubilay Yazoğlu <kyaz...@gmail.com> wrote:
Hello. I have two models. In one of them, I declared a foreign key to the other one. Since this is done in Django by only specifying the Class name, without specifying the field name(column name), when I try to create an object in admin panel, it pulls the wrong column information.

Two models A and B.
B has a Foreignkey for accessing A.
A has two fields Field1 and Field2.
When I try to create an instance of B, in the field of Foreignkey, there is a drop down menu but the content is not from the right field of A.

How to specify it?

--
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...@googlegroups.com.

Matemática A3K

unread,
Nov 27, 2017, 3:31:29 PM11/27/17
to django...@googlegroups.com
On Mon, Nov 27, 2017 at 10:50 AM, Kubilay Yazoğlu <kyaz...@gmail.com> wrote:
Thanks for your answer. I have one more question. What if there are more than one primary keys in that table?

By definition, that won't happen, a primary key is unique. If you add a record with the same key Django will raise an IntegrityError.
 
I'm talking about the unique_together case.

I don't understand you
 
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Matemática A3K

unread,
Nov 27, 2017, 3:38:18 PM11/27/17
to django...@googlegroups.com
On Mon, Nov 27, 2017 at 5:30 PM, Matemática A3K <matemat...@gmail.com> wrote:


On Mon, Nov 27, 2017 at 10:50 AM, Kubilay Yazoğlu <kyaz...@gmail.com> wrote:
Thanks for your answer. I have one more question. What if there are more than one primary keys in that table?

By definition, that won't happen, a primary key is unique. If you add a record with the same key Django will raise an IntegrityError.
 
I'm talking about the unique_together case.

I don't understand you

The ForeignKey uses the model's auto primary key to "link", if you define other keys, np. You should read https://docs.djangoproject.com/en/1.11/ref/models/fields/#foreignkey

The __str__() representation of the object is independent of the keys you define on it. If you want to tweak what is displayed in the drop-down, use this method to show whatever you need :)
Message has been deleted

Kubilay Yazoğlu

unread,
Nov 27, 2017, 3:45:04 PM11/27/17
to Django users
Thanks. I was talking about the composite primary keys in my second question. I've seen that there is a library for it. So, no problem now. Thank you.

27 Kasım 2017 Pazartesi 23:31:29 UTC+3 tarihinde Matemática A3K yazdı:


Reply all
Reply to author
Forward
0 new messages