Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Oct. 18, 2005 03:09:25

Jeremy D.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Models over views?


I have a table (which I don't control) and would like to represent a
calculated column as a (read-only) field. I figure a model over a
view or a field over a function result would work, but am not sure how
to do either.

Any ideas?

Offline

#2 Oct. 26, 2005 03:46:08

[EMAIL P.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Models over views?


Adrian Holovaty wrote:

> Yeah, you could totally just write the model over the view -- I've
> done that before.
>
> """
> CREATE VIEW foo AS SELECT field_one, field_two FROM bar;
>
> class Foo(meta.Model):
> field_one = meta.CharField(...)
> field_two = meta.CharField(...)
> """

What about the ID, though?

I have a view created this way:
create view tags_sorted as select count(*) as c, tag from tags_tags;

And created a model to reflect it. But I can't use it because django
can't select an id from the view. So what's a handy way to get around
that?

Thanks
--B

Offline

#3 Oct. 27, 2005 12:21:55

[EMAIL P.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Models over views?


Nevermind. It occurred to me yesterday to use a OneToOneField, so
there's no ID required:

create view tags_sortedtags as select tag_id, count(*) as cnt from
tags_articles_tags group by tag_id;

class SortedTag(meta.Model):
tag = meta.OneToOneField(Tag)
cnt = meta.PositiveSmallIntegerField()

and that works perfectly.

duh.

--B

Offline

Board footer

Moderator control

Enjoy the 20th of November
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support