Re: Should I use generic foreign key, and how?

2014-08-17 Thread Amirouche Boubekki
2014-08-17 11:24 GMT+02:00 Vladimir Chukharev 
:

>
>
> On Saturday, August 16, 2014 10:46:33 PM UTC+3, Aaron Law wrote:
>>
>>
>> Hi all,
>>
>> I have php web programming background, and new to Django. I am helping my
>> brother to build an online system to manage inventory, and I've got a
>> database design & coding problem recently. Needing help!
>>
>> That is, I have a set of tables of "possible products", "inventory",
>> "suppiler", etc. I want to attach a "comment" to each of them. Therefore, I
>> think I should make a generic "comment" table to hold all the comments of
>> products, inventory, suppiler.
>>
>> --
>> possible_products
>> id: int (pk)
>> name: varchar
>> url: varchar
>> price: decimal
>> created_at: datetime
>> updated_at: datetime
>> --
>> comments
>> id: int (pk)
>> parent_id: int (fk)
>> content: text
>> author: int (fk)
>> table: char (which the table this comment belongs to)
>> created_at: datetime
>> updated_at: datatime
>> --
>>
>> My problem is, when "possible_products" table is the parent of "comment",
>> then a foreign key of the parent (id number) is stored in the "comment"
>> table in order to link up them.
>>
>
>> However, how about the fk of the "inventory" table and the "suppiler"
>> table? It should not be that: I create 2 more columns of "inventory_id" nor
>> "suppiler_id" in "comments" table. (That is, I should not create the 4th
>> column when I want to link up the 4th parent table.)
>>
>
> You can make the fk to the "possible_products" table. In your particular
> case, I do not see reasons to take comments into a separate table rather
> than in the parent table, though this might be just omitted in the
> description.
>

I second that. I don't understand why you need to make another table to
store comments.


> I do use this style of inheriting tables with connections between them. If
> you are interested, I have one project in this style in googlecode (early
> stage yet): labman2 .
>
> Note that django docs generally discourage using the involved type on
> table inheritance. I attribute this discourage to wide use of mysql...
>
>
>> So, Should I use generic forgien key of Django? (so that, my "comment"
>> table design is right.) And how to use/implement it?
>>
>> P.S I mainly develop the system in the Admin section of Django.
>>
>> Regards,
>> Aaron Law Ho hon
>> --~--~-~--~~~---~--~~
>> Free as in Freedom ;-)
>> --~--~-~--~~~---~--~~
>>
>  --
> 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/93d80efa-8ef3-426f-a492-f19222c6a4cc%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/CAL7_Mo8bRX0vAX5hzF_-ajcU4MmPtufJKZ3MQU%3D0dLGNc-Yf9A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Should I use generic foreign key, and how?

2014-08-17 Thread Vladimir Chukharev


On Saturday, August 16, 2014 10:46:33 PM UTC+3, Aaron Law wrote:
>
>
> Hi all,
>
> I have php web programming background, and new to Django. I am helping my 
> brother to build an online system to manage inventory, and I've got a 
> database design & coding problem recently. Needing help!
>
> That is, I have a set of tables of "possible products", "inventory", 
> "suppiler", etc. I want to attach a "comment" to each of them. Therefore, I 
> think I should make a generic "comment" table to hold all the comments of 
> products, inventory, suppiler.
>
> --
> possible_products
> id: int (pk)
> name: varchar
> url: varchar
> price: decimal
> created_at: datetime
> updated_at: datetime
> --
> comments
> id: int (pk)
> parent_id: int (fk)
> content: text
> author: int (fk)
> table: char (which the table this comment belongs to)
> created_at: datetime
> updated_at: datatime
> --
>
> My problem is, when "possible_products" table is the parent of "comment", 
> then a foreign key of the parent (id number) is stored in the "comment" 
> table in order to link up them. 
>

> However, how about the fk of the "inventory" table and the "suppiler" 
> table? It should not be that: I create 2 more columns of "inventory_id" nor 
> "suppiler_id" in "comments" table. (That is, I should not create the 4th 
> column when I want to link up the 4th parent table.)
>

You can make the fk to the "possible_products" table. In your particular 
case, I do not see reasons to take comments into a separate table rather 
than in the parent table, though this might be just omitted in the 
description. I do use this style of inheriting tables with connections 
between them. If you are interested, I have one project in this style in 
googlecode (early stage yet): labman2 .

Note that django docs generally discourage using the involved type on table 
inheritance. I attribute this discourage to wide use of mysql...


> So, Should I use generic forgien key of Django? (so that, my "comment" 
> table design is right.) And how to use/implement it?
>
> P.S I mainly develop the system in the Admin section of Django.
>
> Regards,
> Aaron Law Ho hon
> --~--~-~--~~~---~--~~
> Free as in Freedom ;-)
> --~--~-~--~~~---~--~~
>  

-- 
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/93d80efa-8ef3-426f-a492-f19222c6a4cc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Should I use generic foreign key, and how?

2014-08-17 Thread Amirouche Boubekki
2014-08-16 21:45 GMT+02:00 Aaron Law :

>
> Hi all,
>
> I have php web programming background, and new to Django. I am helping my
> brother to build an online system to manage inventory, and I've got a
> database design & coding problem recently. Needing help!
>
> That is, I have a set of tables of "possible products", "inventory",
> "suppiler", etc. I want to attach a "comment" to each of them. Therefore, I
> think I should make a generic "comment" table to hold all the comments of
> products, inventory, suppiler.
>
> --
> possible_products
> id: int (pk)
> name: varchar
> url: varchar
> price: decimal
> created_at: datetime
> updated_at: datetime
> --
> comments
> id: int (pk)
> parent_id: int (fk)
> content: text
> author: int (fk)
> table: char (which the table this comment belongs to)
> created_at: datetime
> updated_at: datatime
> --
>
> My problem is, when "possible_products" table is the parent of "comment",
> then a foreign key of the parent (id number) is stored in the "comment"
> table in order to link up them.
>
> However, how about the fk of the "inventory" table and the "suppiler"
> table? It should not be that: I create 2 more columns of "inventory_id" nor
> "suppiler_id" in "comments" table. (That is, I should not create the 4th
> column when I want to link up the 4th parent table.)
>
> So, Should I use generic forgien key of Django? (so that, my "comment"
> table design is right.) And how to use/implement it?
>

You SHOULD rather create an abstract ProductBase model class with a
ManyToMany Field pointing to Comment class and then inherit ProductBase for
each specific products.

Most of the time class names are singular.


> P.S I mainly develop the system in the Admin section of Django.
>

See also: django polymorphic.

-- 
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/CAL7_Mo-E1-ZNBk9%2Bh6ggKfs335Z6ai2r7xuUrRr8wYr9YB9b3g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Should I use generic foreign key, and how?

2014-08-17 Thread Davide Scatto
another way is not to use fk but generic field, where patent_id is a normal 
integer field. In a custom manager you have to manage the data content in 
this field with data content in table field to retrieve yr linked record.

Il giorno sabato 16 agosto 2014 21:46:33 UTC+2, Aaron Law ha scritto:
>
>
> Hi all,
>
> I have php web programming background, and new to Django. I am helping my 
> brother to build an online system to manage inventory, and I've got a 
> database design & coding problem recently. Needing help!
>
> That is, I have a set of tables of "possible products", "inventory", 
> "suppiler", etc. I want to attach a "comment" to each of them. Therefore, I 
> think I should make a generic "comment" table to hold all the comments of 
> products, inventory, suppiler.
>
> --
> possible_products
> id: int (pk)
> name: varchar
> url: varchar
> price: decimal
> created_at: datetime
> updated_at: datetime
> --
> comments
> id: int (pk)
> parent_id: int (fk)
> content: text
> author: int (fk)
> table: char (which the table this comment belongs to)
> created_at: datetime
> updated_at: datatime
> --
>
> My problem is, when "possible_products" table is the parent of "comment", 
> then a foreign key of the parent (id number) is stored in the "comment" 
> table in order to link up them.
>
> However, how about the fk of the "inventory" table and the "suppiler" 
> table? It should not be that: I create 2 more columns of "inventory_id" nor 
> "suppiler_id" in "comments" table. (That is, I should not create the 4th 
> column when I want to link up the 4th parent table.)
>
> So, Should I use generic forgien key of Django? (so that, my "comment" 
> table design is right.) And how to use/implement it?
>
> P.S I mainly develop the system in the Admin section of Django.
>
> Regards,
> Aaron Law Ho hon
> --~--~-~--~~~---~--~~
> Free as in Freedom ;-)
> --~--~-~--~~~---~--~~
>  

-- 
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/6c2ac6dc-44c0-4681-960b-29983f609469%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Should I use generic foreign key, and how?

2014-08-16 Thread Collin Anderson
I think there really are two ways to do it, as you say.

Either use a GenericForeignKey(), or have multiple ForeignKey(null=True, 
blank=True), each one pointing to a different model. I personally use the 
multiple foreign keys approach, but this is the case that GenericForeignKey 
was designed for.

-- 
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/988b283a-bf12-430c-a6bd-542eac0106fc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Should I use generic foreign key, and how?

2014-08-16 Thread Aaron Law
Hi all,

I have php web programming background, and new to Django. I am helping my
brother to build an online system to manage inventory, and I've got a
database design & coding problem recently. Needing help!

That is, I have a set of tables of "possible products", "inventory",
"suppiler", etc. I want to attach a "comment" to each of them. Therefore, I
think I should make a generic "comment" table to hold all the comments of
products, inventory, suppiler.

--
possible_products
id: int (pk)
name: varchar
url: varchar
price: decimal
created_at: datetime
updated_at: datetime
--
comments
id: int (pk)
parent_id: int (fk)
content: text
author: int (fk)
table: char (which the table this comment belongs to)
created_at: datetime
updated_at: datatime
--

My problem is, when "possible_products" table is the parent of "comment",
then a foreign key of the parent (id number) is stored in the "comment"
table in order to link up them.

However, how about the fk of the "inventory" table and the "suppiler"
table? It should not be that: I create 2 more columns of "inventory_id" nor
"suppiler_id" in "comments" table. (That is, I should not create the 4th
column when I want to link up the 4th parent table.)

So, Should I use generic forgien key of Django? (so that, my "comment"
table design is right.) And how to use/implement it?

P.S I mainly develop the system in the Admin section of Django.

Regards,
Aaron Law Ho hon
--~--~-~--~~~---~--~~
Free as in Freedom ;-)
--~--~-~--~~~---~--~~

-- 
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/CAPJe6ak%2B489KSBANCcuY56wkUGb9Zi36K3h7kiG2NZCK6CsoUg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.