On May 22, 11:28 am, akaariai <akaar...@gmail.com> wrote:
> Hi all,
>
> I am trying to find a best way to solve the following problem:
>
> I have chemicals, each of which has a list of codes and names.
> Chemicals have too many codes and names to put directly into the
> Chemical model.
>
> Example (pseudo) models.py:
>
> class Chemical():
>     pass
>
> class Name():
>     chemical = ForeignKey(Chemical)
>     nametype = Integer # in reality a foreign key
>     name = varchar(50)
>     unique_together(chemical, nametype)
>
> class Code():
>     chemical = ForeignKey(Chemical)
>     codetype = Integer
>     code = varchar(50)
>     unique_together(chemical, codetype)
>
> I need to make listings of these chemicals, (chemical_id, name, code),
> and I need to change dynamically which name and which code to show.
> Naturally the listings need to be searchable.
>
> The most beautiful solution would be the possibility to directly use:
>
> Chemical.objects.filter(code__codetype = 1).filter(name__nametype =
> 1)\
>         .filter(code__code__contains = '123').values
> ('pk','code__code', 'name__name')
>
> but Django orm can't handle this currently. I actually looked into the
> code to see how hard this would be to implement for values and
> values_list. I quickly saw that this is not something you want to do
> as your first Django coding project...
>
> So far I have a few solutions to this problem.
>
> 1) Use raw sql
>     - works, but is ugly
>
> 2) Create a database view and a corresponding model, and use that for
> listings
>     - works, but is after all only marginally better than solution 1.
> Biggest problem is that it is not DRY.
>
> 3) Filter both Name and Code manually, then join them in python.
>     - I haven't really tried this one. I think this is the ugliest so
> far.
>
> I am currently using solution 2. but I am wondering if there is some
> better way to do this? Maybe using .extra() in some way?
>
> By the way, I have just switched to Django from JDBC + jsp. I can't
> believe how easy web programming can be with the right tools. Thank
> you!


If the problem is just the values_list call, you could easily get the
values into a list yourself using your existing query:

chemicals = Chemical.objects.filter(
            code__codetype=1, name__nametype=1,
            code__code__contains = '123'
        ).select_related()
values = [(c.pk, c.code.code, c.name.name) for c in chemicals]

There's a slight overhead here in instatiating all the chemical
objects, but it's not significant. You will need select_related, as
I've shown above, to avoid making multiple queries though.
--
DR.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@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