Is the logic in my model correct? Best-case?

30 views
Skip to first unread message

Barry Morrison

unread,
Sep 3, 2012, 9:51:45 PM9/3/12
to django...@googlegroups.com
Model: http://dpaste.org/dqwmc/

There can be many products.
There can be many distributors.
A product could have none or more distributors.
A distributor could only distribute a single product once.
A distributor could distribute all products. 

I imagine a form (admin side):

Product Name:
Product Description:
Product Image:

Distributor 1: Radio Field/Boolean
Distributor 2: Radio Field/Boolean
Etc. etc.

For some reason I can't wrap my head around this. 

Thanks!
Message has been deleted

Barry Morrison

unread,
Sep 4, 2012, 8:05:47 AM9/4/12
to django...@googlegroups.com
That's exactly what I needed.  Thank you *very* much! 

On Mon, Sep 3, 2012 at 8:44 PM, Dennis Lee Bieber <wlf...@ix.netcom.com> wrote:
On Mon, 3 Sep 2012 18:51:45 -0700 (PDT), Barry Morrison
<bdmor...@gmail.com> declaimed the following in
gmane.comp.python.django.user:


> Model: http://dpaste.org/dqwmc/
>
> There can be many products.

        Okay, a table of products...


> There can be many distributors.

        and a table of distributors


> A product could have none or more distributors.

        Normal intersect table behavior


> A distributor could only distribute a single product once.

        This clause is unclear...


> A distributor could distribute all products.
>

        This clause is irrelevant -- it doesn't restrict the data (vs if you
had some requirement that a distributor carrying products a..m is not
allowed to carry product n)


> I imagine a form (admin side):
>
> Product Name:
> Product Description:
> Product Image:
>
> Distributor 1: Radio Field/Boolean
> Distributor 2: Radio Field/Boolean
> Etc. etc.
>
> For some reason I can't wrap my head around this.

        Your model (per the linked web page) current ties a product instance
to ONE DISTRIBUTOR ONLY.

        In pseudo-SQL, you have defined

create table Distributor
(
        ID      integer autoincrement primary key,      #django adds this
        Name    varchar(255) not null,  #it is redundant to preface fields
                                                                        #with "distributor" when they are
                                                                        #IN the table Distributor
        URL     varchar(255) null,              #URL type is django operation
        Product boolean                 #this field is meaningless as
                                                                        #there is no product referenced
                                                                        #here
);

create table Product
(
        ID      integer autoincrement primary key,
        Name    varchar(255) not null,
        Description     blob,   #or some such large text field
        Image   varchar(255) null,      #image type is django usage
        Distributor     integer foreign key references Distributor (ID),
                        #this basically says that this product can have
                        #only ONE distributor
);

        What you really need is not a one-to-many link (one distributor can
have many products, but each product can only have one distributor) but
a many-to-many linkage.

create table Distributor
(
        ID      integer autoincrement primary key,      #django adds this
        Name    varchar(255) not null,
        URL     varchar(255) null,
);

create table Product
(
        ID      integer autoincrement primary key,
        Name    varchar(255) not null,
        Description     blob,   #or some such large text field
        Image   varchar(255) null,      #image type is django usage
);

create table ProductDistribution
(
        ID      integer autoincrement primary key,
        ProductID       integer foreign key references Product(ID),
        DistributorID   integer foreign key references Distributor(I),
        unique (ProductID, DistributorID)
);

        As I understand it, Django will create the equivalent of
ProductDistribution when you declare a many-to-many relationship in one
of the parent tables.

--
        Wulfraed                 Dennis Lee Bieber         AF6VN
        wlf...@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


Barry Morrison

unread,
Sep 6, 2012, 2:59:13 PM9/6/12
to django...@googlegroups.com, wlf...@ix.netcom.com
So I created models: http://dpaste.org/D4r7L/ forms: http://dpaste.org/KMShj/ the output of the form generates: http://dpaste.org/ua41K/

The form creates text areas to enter distributor name/url.  What I'd like on this form is a set of radio buttons to allow a true/false if the product is distributed by a specific distributor. 

Do I need to create another field on the Distributor model and set a boolean field/widget?

Thanks,
Barry


On Monday, September 3, 2012 8:45:07 PM UTC-7, Dennis Lee Bieber wrote:
On Mon, 3 Sep 2012 18:51:45 -0700 (PDT), Barry Morrison
<bdmor...@gmail.com> declaimed the following in
gmane.comp.python.django.user:

> Model: http://dpaste.org/dqwmc/
>
> There can be many products.

        Okay, a table of products...

> There can be many distributors.

        and a table of distributors

> A product could have none or more distributors.

        Normal intersect table behavior

> A distributor could only distribute a single product once.

        This clause is unclear...

> A distributor could distribute all products.  
>

        This clause is irrelevant -- it doesn't restrict the data (vs if you
had some requirement that a distributor carrying products a..m is not
allowed to carry product n)
 
> I imagine a form (admin side):
>
> Product Name:
> Product Description:
> Product Image:
>
> Distributor 1: Radio Field/Boolean
> Distributor 2: Radio Field/Boolean
> Etc. etc.
>
> For some reason I can't wrap my head around this.  

Barry Morrison

unread,
Sep 6, 2012, 3:54:03 PM9/6/12
to django...@googlegroups.com, wlf...@ix.netcom.com
I added this instead to the forms:

class ShopProductDistributionForm(ModelForm):
    """A partial form to allow the association to distributors"""
    class Meta:
        model = ProductDistribution
        fields = ['productID', 'distributorID']

    def __init__(self, *args, **kwargs):
        self.helper = FormHelper()

        self.helper.form_tag = False
        self.helper.layout = Layout(
            Field('productID'),
            Field('distributorID', type='radio'),)

        super(ShopProductDistributionForm, self).__init__(*args, **kwargs)


The problem is, it creates drop-down/select fields instead of allowing me to create radio buttons.  I guess I need to figure out how to parse through the distributors. 
Reply all
Reply to author
Forward
Message has been deleted
0 new messages