Hmmm.  Probably just as ugly, and untested, but:

class A(basemodel):
    for o in range(MAXNUMREDPORTS):
         for i in (1,2):
             locals()["Port%d_redfield%d" % (o+1, i)] = models.FloatField()
...

On Mon, Dec 7, 2009 at 12:10 PM, Daniel Goertzen
<daniel.goert...@gmail.com> wrote:
> Okay, I explored model generation a bit and found an approach that wasn't
> too offensive.  For those that are trying to solve the same problem that I
> am, here is what I came up with:
>
> def red_port(prefix):
>    return """
> %(prefix)sredfield1 = models.FloatField()
> %(prefix)sredfield2 = models.FloatField()
> """ % {'prefix':prefix}
> class A(basemodel):
>   exec red_port("Port1_")
>   exec red_port("Port2_")
>   ...
>
> ...which yields sql something like...
> CREATE TABLE `A` (
>     `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
>     ...
>     `Port1_redfield1` double precision,
>     `Port1_redfield2` double precision,
>     `Port2_redfield1` double precision,
>     `Port2_redfield2` double precision,
> );
>
>
> I feel DRY again, and only a little bit dirty. :)
> Dan.
>
> On Mon, Dec 7, 2009 at 9:12 AM, Dan <daniel.goert...@gmail.com> wrote:
>>
>> I am relatively new to django and relational databases, and need some
>> Model advice.
>>
>> I have a family of network products each with a different combination
>> of ports.. I will call them red ports and green ports.  For example,
>> product A will have its own Model and could have 2 red ports and 1
>> green port.  Product B is similar, but has only 3 green ports.
>> My current approach is to use an abstract base Model, and then create
>> Models for products A and B that contain all needed fields, for
>> example:
>>
>> class A(basemodel):
>>   Port1_redfield1 = ...
>>   Port1_redfield2 = ...
>>   ...
>>   Port2_redfield1 = ...
>>   Port2_redfield2 = ...
>>   ...
>>
>>
>> Yuck, not very DRY at all.  I also tried to create a red port Model
>> and green port Model and use one to one fields to connect them to
>> products.  The problem with this is that I could not figure out how to
>> encapsulate the ports; I always had to worry about creating, linking
>> and deleting port objects when dealing with products.  Not acceptable.
>>
>> I also thought about other tricks like generating my models.py file
>> from a generator program, and dynamic insertion of attributes into
>> existing classes.  I really would rather not go there.
>>
>> So, my question:  Is there a nice DRY way to get the kind of
>> structured composition that I want?
>>
>> Side question: Is there another python ORM that could address my need
>> better?
>>
>> Thanks,
>> Dan.
>
>
> --
> Daniel Goertzen
> -----------------
> d...@networkintegritysystems.com (work)
> daniel.goert...@gmail.com (home)
> -----------------
> 1 204 272 6149 (home/office)
> 1 204 470 8360 (mobile)
> -----------------
>
>
>
> --
>
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-us...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>

--

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


Reply via email to