python sql query in django

Discussion in 'Python' started by May, Feb 23, 2009.

  1. May

    May Guest

    I have three tables:

    class Technology(models.Model):
    technology = models.CharField(max_length=100, null=True,
    blank=True )
    def __unicode__(self):
    return self.technology
    class Meta:
    ordering = ["technology"]

    class Publication(models.Model):
    pubtitle = models.TextField(null=True, blank=True)
    def __unicode__(self):
    return self.pubtitle
    class Meta:
    ordering = ["pubtitle"]

    class Techpubcombo(models.Model):
    technology = models.ForeignKey(Technology)
    publication = models.ForeignKey(Publication)

    The user selects a technology from the drop down menu on the web
    page. The technology is retrieved
    from the database table and then it must be used to retrieve the
    publication attributes, by first going through the Techpubcombo table.

    I wrote the select to retrieve the data for the html drop down box:
    technology_list = Technology.objects.all().order_by('technology')

    After the user makes a selection, the technology_id is retrieved:
    technology_id = request.POST['technology_id']
    t = get_object_or_404(Technology, pk=technology_id)

    Now I need to use the technology_id to get to the publication
    attributes

    This is where I'm stuck.

    I get error messages when I try to do something like:

    pub=t.techpubcombo.publications_set()

    Ana
    May, Feb 23, 2009
    #1
    1. Advertising

  2. May a écrit :
    > I have three tables:


    Actually - from Python's code POV - three Model classes. And actually,
    since there's a very active, friendly and helpful django group on
    googlegroups, you'd be better reposting your question there.

    (snip Django's ORM related question)
    Bruno Desthuilliers, Feb 23, 2009
    #2
    1. Advertising

  3. May

    May Guest

    On Feb 23, 11:31 am, Bruno Desthuilliers
    <> wrote:
    > May a écrit :
    >
    > > I have three tables:

    >
    > Actually - from Python's code POV - three Model classes. And actually,
    > since there's a very active, friendly and helpful django group on
    > googlegroups, you'd be better reposting your question there.
    >
    > (snip Django's ORM related question)


    The django users groups suggests using a manytomany field. That works
    well in the html template, but in the admin tables, the logic for the
    manytomany field applies to the wrong model and doesn't create a
    choice for the data entry people. I'm trying to write this without
    using the manytomany option. Just using the standard relational
    database model. I need to understand if python can do what I need to
    do. So far, I've not been successful with an answer in users groups.

    Ana
    May, Feb 23, 2009
    #3
  4. May a écrit :
    (snip)
    > I may not stay with Django.


    Nope, but your question was about Django.

    > I am seriously looking for whether python
    > can read data from a relational database


    Of course - as long as there's a Python adapter for your DB.

    > and send to an html template


    Of course - as long as it's either a Python templating system or there's
    a Python binding for this system.

    > or do I always need some kind of wrapper/interface such as Rails


    Uh ? Rails is a Ruby framework.

    > or
    > Django?


    No, you don't. If you prefer to reinvent the wheel on each and any part
    of each an any web app you write, please feel free to do so.

    > If this is the wrong group to ask that question


    Which "that" question ?-)

    For question related to Django's ORM, the django group is indeed a
    better place (as usual: always use the most specific group / maling list
    / whatever first). If you have questions (like the above) that are about
    Python itself, you're at the right place.
    Bruno Desthuilliers, Feb 23, 2009
    #4
  5. May

    Steve Holden Guest

    May wrote:
    > On Feb 23, 11:31 am, Bruno Desthuilliers
    > <> wrote:
    >> May a écrit :
    >>
    >>> I have three tables:

    >> Actually - from Python's code POV - three Model classes. And actually,
    >> since there's a very active, friendly and helpful django group on
    >> googlegroups, you'd be better reposting your question there.
    >>
    >> (snip Django's ORM related question)

    >
    > The django users groups suggests using a manytomany field. That works
    > well in the html template, but in the admin tables, the logic for the
    > manytomany field applies to the wrong model and doesn't create a
    > choice for the data entry people. I'm trying to write this without
    > using the manytomany option. Just using the standard relational
    > database model. I need to understand if python can do what I need to
    > do. So far, I've not been successful with an answer in users groups.
    >

    I am sure if you explain in detail exactly what you want the admin to
    present to your data entry people there are those on django-users who
    can help you make it happen. That list *would* be the best place to get
    an answer: this one mostly discusses the Python language in a more
    generic manner.

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC http://www.holdenweb.com/
    Steve Holden, Feb 23, 2009
    #5
  6. May

    May Guest

    On Feb 23, 1:00 pm, Steve Holden <> wrote:
    > May wrote:
    > > On Feb 23, 11:31 am, Bruno Desthuilliers
    > > <> wrote:
    > >> May a écrit :

    >
    > >>> I have three tables:
    > >> Actually - from Python's code POV - three Model classes. And actually,
    > >> since there's a very active, friendly and helpful django group on
    > >> googlegroups, you'd be better reposting your question there.

    >
    > >> (snip Django's ORM related question)

    >
    > > The django users groups suggests using a manytomany field.  That works
    > > well in the html template, but in the admin tables, the logic for the
    > > manytomany field applies to the wrong model and doesn't create a
    > > choice for the data entry people.  I'm trying to write this without
    > > using the manytomany option.  Just using the standard relational
    > > database model.  I need to understand if python can do what I need to
    > > do.  So far, I've not been successful with an answer in users groups.

    >
    > I am sure if you explain in detail exactly what you want the admin to
    > present to your data entry people there are those on django-users who
    > can help you make it happen. That list *would* be the best place to get
    > an answer: this one mostly discusses the Python language in a more
    > generic manner.
    >
    > regards
    >  Steve
    > --
    > Steve Holden        +1 571 484 6266   +1 800 494 3119
    > Holden Web LLC              http://www.holdenweb.com/


    I may not stay with Django. I am seriously looking for whether python
    can read data from a relational database and send to an html template
    or do I always need some kind of wrapper/interface such as Rails or
    Django? If this is the wrong group to ask that question could you
    recommend another python group that could?

    Thanks.
    May, Feb 23, 2009
    #6
  7. > I may not stay with Django. I am seriously looking for whether python
    > can read data from a relational database and send to an html template
    > or do I always need some kind of wrapper/interface such as Rails or
    > Django? If this is the wrong group to ask that question could you
    > recommend another python group that could?


    Python itself comes neither with an ORM, nor with a HTML
    generating/mapping framework.

    So yes, if you want something like that, need something like rails or
    django. Or TurboGears.

    For example, you can use SQLAlchemy as ORM, and RUM as frontend for it.
    I'm not sure if it fullfills your requirements though.

    http://python-rum.org/

    Diez
    Diez B. Roggisch, Feb 23, 2009
    #7
  8. On Feb 23, 2009, at 4:34 PM, Diez B. Roggisch wrote:

    >> I may not stay with Django. I am seriously looking for whether
    >> python
    >> can read data from a relational database and send to an html template
    >> or do I always need some kind of wrapper/interface such as Rails or
    >> Django? If this is the wrong group to ask that question could you
    >> recommend another python group that could?

    >
    > Python itself comes neither with an ORM, nor with a HTML generating/
    > mapping framework.
    >
    > So yes, if you want something like that, need something like rails
    > or django. Or TurboGears.
    >
    > For example, you can use SQLAlchemy as ORM, and RUM as frontend for
    > it. I'm not sure if it fullfills your requirements though.


    And if you don't like ORMs, most databases have at least one adapter
    out there that allows one to send straight SQL to the DB engine and
    get results back in some useful form. For instance, there's the
    psycopg2 adapter for talking to Postgres. Python has a built-in
    wrapper for SQLite.
    Philip Semanchuk, Feb 23, 2009
    #8
  9. May

    May Guest

    On Feb 23, 12:48 pm, Bruno Desthuilliers
    <> wrote:
    > May a écrit :
    > (snip)
    >
    > > I may not stay with Django.

    >
    > Nope, but your question was about Django.
    >
    > > I am seriously looking for whether python
    > > can read data from a relational database

    >
    > Of course - as long as there's a Python adapter for your DB.
    >
    > > and send to an html template

    >
    > Of course - as long as it's either a Python templating system or there's
    > a Python binding for this system.
    >
    > > or do I always need some kind of wrapper/interface such as Rails

    >
    > Uh ? Rails is a Ruby framework.
    >
    > > or
    > > Django?  

    >
    > No, you don't. If you prefer to reinvent the wheel on each and any part
    > of each an any web app you write, please feel free to do so.
    >
    > > If this is the wrong group to ask that question

    >
    > Which "that" question ?-)
    >
    > For question related to Django's ORM, the django group is indeed a
    > better place (as usual: always use the most specific group / maling list
    > / whatever first). If you have questions (like the above) that are about
    > Python itself, you're at the right place.


    Thanks for all your suggestions. From what I've experienced in Django
    and now that I know a little more about how Python functions, I will
    probably use a combination of PHP and Django, instead of trying to get
    Python to do the web portion of my project. Thanks again!
    May, Feb 24, 2009
    #9
  10. >
    > Thanks for all your suggestions. From what I've experienced in Django
    > and now that I know a little more about how Python functions, I will
    > probably use a combination of PHP and Django, instead of trying to get
    > Python to do the web portion of my project. Thanks again!


    That sounds like the worst idea. Django's ORM is good when used from
    within django, because of all the additional goodies like the admin
    interface.

    But if you are going to do the webfrontend using PHP, what part is left
    for django? If it's only the ORM (for whatever you still use that
    anyway), there are better alternatives - SQLAlchemy, and SQLObject, for
    Python. They are more powerful and not interwoven with django.


    If you are going to hand-code the interface in PHP, I fail to see though
    why you don't do that in Django directly. You are not forced to use the
    Admin-interface.

    Diez
    Diez B. Roggisch, Feb 24, 2009
    #10
  11. May

    May Guest

    On Feb 24, 10:36 am, "Diez B. Roggisch" <> wrote:
    > > Thanks for all your suggestions.  From what I've experienced in Django
    > > and now that I know a little more about how Python functions, I will
    > > probably use a combination of PHP and Django, instead of trying to get
    > > Python to do the web portion of my project.  Thanks again!

    >
    > That sounds like the worst idea. Django's ORM is good when used from
    > within django, because of all the additional goodies like the admin
    > interface.
    >
    > But if you are going to do the webfrontend using PHP, what part is left
    > for django? If it's only the ORM (for whatever you still use that
    > anyway), there are better alternatives - SQLAlchemy, and SQLObject, for
    > Python. They are more powerful and not interwoven with django.
    >
    > If you are going to hand-code the interface in PHP, I fail to see though
    > why you don't do that in Django directly. You are not forced to use the
    > Admin-interface.
    >
    > Diez


    Hello Diez,

    I think Django is fabulous for the admin-interface, a simple text
    search and template inheritance. I will use Django for all of those.
    What I'm not getting an answer to and cannot find an example of is a
    complex search, where I have to retrieve data from multiple tables,
    combine the data, remove the duplicates, etc between a web page and
    the database. The code that started this thread is only a small piece
    of the complex data retrieval I need to do. PHP is great for writing
    complex SQL queries right in the HTML template and I know exactly what
    it is doing.
    May, Feb 24, 2009
    #11
  12. May a écrit :
    > On Feb 24, 10:36 am, "Diez B. Roggisch" <> wrote:
    >>> Thanks for all your suggestions. From what I've experienced in Django
    >>> and now that I know a little more about how Python functions, I will
    >>> probably use a combination of PHP and Django, instead of trying to get
    >>> Python to do the web portion of my project. Thanks again!

    >> That sounds like the worst idea. Django's ORM is good when used from
    >> within django, because of all the additional goodies like the admin
    >> interface.
    >>
    >> But if you are going to do the webfrontend using PHP, what part is left
    >> for django? If it's only the ORM (for whatever you still use that
    >> anyway), there are better alternatives - SQLAlchemy, and SQLObject, for
    >> Python. They are more powerful and not interwoven with django.
    >>
    >> If you are going to hand-code the interface in PHP, I fail to see though
    >> why you don't do that in Django directly. You are not forced to use the
    >> Admin-interface.
    >>
    >> Diez

    >
    > Hello Diez,
    >
    > I think Django is fabulous for the admin-interface, a simple text
    > search and template inheritance.


    And I think you really don't get what Django is good for.

    > I will use Django for all of those.
    > What I'm not getting an answer to and cannot find an example of is a
    > complex search, where I have to retrieve data from multiple tables,
    > combine the data, remove the duplicates, etc


    Django's ORM is mostly a wrapper above the db-api. It's intended to make
    most common db access a no-brainer, not to replace full-blown SQL for
    complex things. Now the good news is that you *still* can drop to a
    lower raw-sql level - the one you'd get using either PHP or Python's
    db-api - for more complex things. IOW, PHP won't buy you anything here.

    > The code that started this thread is only a small piece
    > of the complex data retrieval I need to do.


    The "code that started this thread" is a total no-brainer using Django
    (as you would know by now if you had reposted your question on django's
    group - or even just read the FineManual, where this use case is fully
    documented), and doesn't even require going down to raw sql.

    > PHP is great for writing
    > complex SQL queries right in the HTML template


    What the .... a complex SQL query has to do in a template anyway ???
    Sorry, but what you're saying here is that PHP is great for writing
    unmaintainable spaghetti code. FWIW, even PHP coders are trying to get
    out of this kind of mess nowadays.
    Bruno Desthuilliers, Feb 25, 2009
    #12
  13. > I think Django is fabulous for the admin-interface, a simple text
    > search and template inheritance. I will use Django for all of those.
    > What I'm not getting an answer to and cannot find an example of is a
    > complex search, where I have to retrieve data from multiple tables,
    > combine the data, remove the duplicates, etc between a web page and
    > the database. The code that started this thread is only a small piece
    > of the complex data retrieval I need to do. PHP is great for writing
    > complex SQL queries right in the HTML template and I know exactly what
    > it is doing.


    First of all, mixing technologies without need is most of the times a
    bad idea - so if it is really the case that you can't solve all of your
    issues in django, you shouldn't use it at all, but solve them in PHP.


    But to be honest - I doubt that django isn't capable of solving your
    problem.

    See

    http://docs.djangoproject.com/en/dev/topics/db/queries/#topics-db-queries

    for a introduction to the multitude of query-options. I doubt that your
    rather simple m:n-relationship is covered there.


    I myself use SQLAlchemy, and that can for sure query and filter those
    relationships.

    Diez
    Diez B. Roggisch, Feb 25, 2009
    #13

  14. > for a introduction to the multitude of query-options. I doubt that your
    > rather simple m:n-relationship is covered there.


    s/is/isn't/

    Diez
    Diez B. Roggisch, Feb 25, 2009
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anonymous
    Replies:
    0
    Views:
    1,440
    Anonymous
    Oct 13, 2005
  2. John Posner

    Question on Django and Django Book

    John Posner, Nov 13, 2010, in forum: Python
    Replies:
    0
    Views:
    307
    John Posner
    Nov 13, 2010
  3. Sumit
    Replies:
    2
    Views:
    415
    Dhaivat Pandya
    Feb 22, 2011
  4. eGenix Team: M.-A. Lemburg
    Replies:
    0
    Views:
    126
    eGenix Team: M.-A. Lemburg
    Jun 18, 2013
  5. Gary Roach
    Replies:
    0
    Views:
    125
    Gary Roach
    Jan 2, 2014
Loading...

Share This Page