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

2015-11-05 Thread Jani Tiainen
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.


On Thu, Nov 5, 2015 at 6:12 PM, frocco  wrote:

> Thank you
>
> On Wednesday, November 4, 2015 at 11:19:40 AM UTC-5, frocco wrote:
>>
>> 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
>>
>> --
> 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 http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/f42b087f-dffe-42df-994b-22e9028dd4c7%40googlegroups.com
> 
> .
>
> 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...

-- 
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHn91ofvaaSWR-VPd9%3Dppc%3DOuM4Huo38Hf4zfb%2B-LmDcX0biYQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


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

2015-11-05 Thread frocco
Thank you

On Wednesday, November 4, 2015 at 11:19:40 AM UTC-5, frocco wrote:
>
> 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
>
>

-- 
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/f42b087f-dffe-42df-994b-22e9028dd4c7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


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

2015-11-05 Thread Gergely Polonkai
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

2015-11-05 15:47 GMT+01:00 frocco :

> 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
>
>
> On Wednesday, November 4, 2015 at 11:19:40 AM UTC-5, frocco wrote:
>>
>> 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
>>
>> --
> 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 http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/44660e3a-cd6c-4858-8b29-d1dc4ceeced0%40googlegroups.com
> 
> .
>
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CACczBULUX9nMhjMFgM54ZQ3CZiNNZPLTjBeidO9-f%2BVzR30zGw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


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

2015-11-05 Thread frocco
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


On Wednesday, November 4, 2015 at 11:19:40 AM UTC-5, frocco wrote:
>
> 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
>
>

-- 
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/44660e3a-cd6c-4858-8b29-d1dc4ceeced0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


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

2015-11-05 Thread Gergely Polonkai
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.

2015-11-05 15:08 GMT+01:00 frocco :

> 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
>
> On Wednesday, November 4, 2015 at 12:35:49 PM UTC-5, Dheerendra Rathor
> wrote:
>>
>> In your model use define __str__ method to name1. Then django admin will
>> use name1 in dropdown box.
>>
>> On Wed, 4 Nov 2015 at 21:50 frocco  wrote:
>>
>>> 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
>>>
>>> --
>>> 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.
>>>
>> --
> 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 http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/b76f6937-5be1-4025-8fe0-093f4542a527%40googlegroups.com
> 
> .
>
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CACczBUJHosmjBKanE_0x95412YGjOQ9%3DTkD%3DkdTHwXZuhsB%3DtQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


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

2015-11-05 Thread frocco
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

On Wednesday, November 4, 2015 at 12:35:49 PM UTC-5, Dheerendra Rathor 
wrote:
>
> In your model use define __str__ method to name1. Then django admin will 
> use name1 in dropdown box. 
>
> On Wed, 4 Nov 2015 at 21:50 frocco  wrote:
>
>> 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
>>
>> -- 
>> 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.
>>
>

-- 
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/b76f6937-5be1-4025-8fe0-093f4542a527%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


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

2015-11-04 Thread Gergely Polonkai
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".
On 4 Nov 2015 17:19, "frocco"  wrote:

> 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
>
> --
> 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 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.
>

-- 
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CACczBUJif1WaNEE-EJ4%3DCgwKxPHFNj6nVJy39FmYN3KhXozyug%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


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

2015-11-04 Thread Dheerendra Rathor
In your model use define __str__ method to name1. Then django admin will
use name1 in dropdown box.

On Wed, 4 Nov 2015 at 21:50 frocco  wrote:

> 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
>
> --
> 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 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.
>

-- 
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAByqUghis4%2Bsu8dfCL1NiNaSGUMpCeSrZP9yTMkQ192eohbCsw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


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

2015-11-04 Thread frocco
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

-- 
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 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.