Tuesday, February 23, 2016

Relations one-to-many on django

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.
 

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/8a048337-0c19-4266-8b17-87235cd35daa%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment