Relations one-to-many on django

36 views
Skip to first unread message

Douglas Bonafe

unread,
Feb 23, 2016, 4:13:18 PM2/23/16
to Django users
Hello everybody!
I'm new on this forum and in django.
I'm using Python 2.7 environment.

I have the following models.py:

class Categoria(models.Model):

    DEFAULT_CATHEGORY = 0
    nome = models.CharField(max_length=255, null=False, default='0')

    def __unicode__(self):
        return self.nome

class Produto(models.Model):

    nome = models.CharField(max_length=255, null=False)
    pontos_na_compra = models.DecimalField(decimal_places=4, max_digits=10, default=0)
    pontos_na_troca = models.DecimalField(decimal_places=4, max_digits=10, default=0)
    foto = models.ImageField(upload_to=get_image_path, blank=True, null=True)

    categoria = models.ForeignKey(
        Categoria,
        default=Categoria.DEFAULT_CATHEGORY
    )

    def __unicode__(self):
        return "%s, %s" % (self.nome, self.codigo_depara)

I want the relation: each Categoria (cathegory) has many Produto (products). Is it correct in the model?
I don't have anything on views.py relationed to it.

I want to do some code like this into HTML file:

show CATEGORIA.nome
for each PRODUTO in LIST OF PRODUCTS IN CATEGORIA do:
   show PRODUTO.nome
   show PRODUTO.pontos_na_compra
   show PRODUTO.foto
end-for
 
I try to use
{% if categorias %}
                            <ul id="flexisel">
                        {% for categoria in produto.categoria_set() %}
                                <li>
                                    # SHOW ALL HERE. HOW?
                                </li>
                        {% endfor %}
                        </ul>
                  {% else %}
                  <p id="flexise">
                     Nenhum produto encontrado!
                  </p>
                  {% endif %}

But it didn't find the categoria_set() method and returns this:

Could not parse the remainder: '()' from 'produto.categoria_set()'
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 1.9.2
Exception Type: TemplateSyntaxError
Exception Value:
Could not parse the remainder: '()' from 'produto.categoria_set()'
Exception Location: /home/dsbonafe/.virtualenvs/bruno/local/lib/python2.7/site-packages/django/template/base.py in parse, line 516
Python Executable: /home/dsbonafe/.virtualenvs/bruno/bin/python
Python Version: 2.7.6
Python Path:
['/home/dsbonafe/Documentos/Projetos/Bruno Mercado/bruno/redewebsite',
 '/home/dsbonafe/.virtualenvs/bruno/lib/python2.7',
 '/home/dsbonafe/.virtualenvs/bruno/lib/python2.7/plat-x86_64-linux-gnu',
 '/home/dsbonafe/.virtualenvs/bruno/lib/python2.7/lib-tk',
 '/home/dsbonafe/.virtualenvs/bruno/lib/python2.7/lib-old',
 '/home/dsbonafe/.virtualenvs/bruno/lib/python2.7/lib-dynload',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-x86_64-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/home/dsbonafe/.virtualenvs/bruno/local/lib/python2.7/site-packages',
 '/home/dsbonafe/.virtualenvs/bruno/lib/python2.7/site-packages']
Server time: Ter, 23 Fev 2016 20:55:39 +0000


Someone could help me?

Thanks a lot.
 

James Schneider

unread,
Feb 23, 2016, 4:59:15 PM2/23/16
to django...@googlegroups.com

>  
> I try to use
>>
>> {% if categorias %}
>>                             <ul id="flexisel">
>>                         {% for categoria in produto.categoria_set() %}

At no time can parentheses be used when referencing variables. The correct way to write this tag would be {% for categoria in produto.categoria_set %}.


Make sure to review the template syntax for accessing context variables and methods: https://docs.djangoproject.com/en/1.9/ref/templates/language/#variables


-James

Vijay Khemlani

unread,
Feb 23, 2016, 5:01:41 PM2/23/16
to django...@googlegroups.com
Each product is only associated with one category, so you can only do 

produto.categoria

If you want to iterate over all categories, and then through the products of each category, you could do

{% for categoria in categorias %}
{% for produto in categoria.produto_set.all %}
do comething with produto
{% endfor %}
{% endfor %}

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciXoFit4aj1sLmNwwiZ2%3DYg2%2Bz2cp0DuNk-cPxxyn8sikA%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages