How do I relate two tables using field name instead of id?

135 views
Skip to first unread message

frocco

unread,
Nov 4, 2015, 4:19:40 PM11/4/15
to Django users
Hello,

I have two existing tables 
table1 has name1

table2 has name2

when I edit table2 in admin, I want a dropdownbox that shows values from table1 using name1
both fields are character

I cannot change the design, porting from msaccess

thanks

Dheerendra Rathor

unread,
Nov 4, 2015, 5:35:49 PM11/4/15
to Django users
In your model use define __str__ method to name1. Then django admin will use name1 in dropdown box. 

--
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.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/756fc995-1c2c-4aee-a074-0a327dbc84e0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Gergely Polonkai

unread,
Nov 4, 2015, 5:42:51 PM11/4/15
to Django users

This really depends on the structure of your models. In this case you will most likely have a ForeignKey in one of your models; if the referenced model has an __str__() method, the admin site (and also any other forms) will display names instead of the text "OtherModel object".

frocco

unread,
Nov 5, 2015, 2:08:10 PM11/5/15
to Django users
The two models from msaccess are linked by name field, not id.
Can I like by name field in django and not use id?

There was no id in the table

Gergely Polonkai

unread,
Nov 5, 2015, 2:35:42 PM11/5/15
to Django users
It is possible, but in an SQL database it may become ineffective. For this, you will have to make the name field unique, and put an index on it (uniqueness also creates an index, by the way).

For the admin site to display names, though, you still have to define the __str__() method that does only something like "return self.name".

However, based on what I said above, if I were you, I would convert the access database so the tables use integer IDs for foreign keys. The admin site will need to fetch data from the other table in both cases, but it can speed up indexing and usage on the long run.

frocco

unread,
Nov 5, 2015, 2:47:43 PM11/5/15
to Django users
Thanks, I will look into adding id

If I just do this, clinician = models.ForeignKey(Clinician)
I get clinician_id does not exist

In my related table I have
def __unicode__(self):
return self.clinicianname

Gergely Polonkai

unread,
Nov 5, 2015, 4:03:57 PM11/5/15
to Django users
If you go with the current solution, you will have to add the to_field keyword argument to ForeignKey:

clinician = models.ForeignKey(Clinician, to_field='name')

Best,
Gergely

--
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.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

frocco

unread,
Nov 5, 2015, 4:12:43 PM11/5/15
to Django users
Thank you


On Wednesday, November 4, 2015 at 11:19:40 AM UTC-5, frocco wrote:

Jani Tiainen

unread,
Nov 5, 2015, 8:36:11 PM11/5/15
to django...@googlegroups.com

You need to set few additional attributes in your foreign key.

First you have to use db_column='', and to_field=''

db_column value would be column name in your table that holds foreign key. I guess this would be 'name2' in your model that reflects table2

to_field would be reference in field in a model that reflects table1 db column name1

So as a simplified example:

class MyModel1(models.Model):
    name1 = model.CharField(max_length=123)

    class Meta:
        db_table = 'table1'

class MyModel2(models.Model):
    name2 = model.ForeignKey('myapp.MyModel1', db_column='name2', to_field='name1')

    class Meta:
        db_table = 'table1'

Hope that helps.


--
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.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

For more options, visit https://groups.google.com/d/optout.



--
Jani Tiainen

- Well planned is half done, and a half done has been sufficient before...
Reply all
Reply to author
Forward
0 new messages