Problems with "Class Meta" and " __str__"

40 views
Skip to first unread message

Elmaco7

unread,
Sep 10, 2019, 12:30:28 PM9/10/19
to Django users
Hello, I do these models but the Django admin page doesn't read the "class Meta" and " __str__".
This is the models.py document

from django.db import models

# Create your models here.


class Autore(models.Model):
    nome = models.CharField(max_length=50)
    cognome = models.CharField(max_length=50)
def __str__(self):
        return self.autore_text
class Meta:
    verbose_name_plural = "Autori"

class Genere(models.Model):
    descrizione = models.CharField(max_length=30)
def __str__(self):
        return self.genere_text
class Meta:
    verbose_name_plural = "Generi"

class Libro(models.Model):
    titolo = models.CharField(max_length=200)
    autore = models.ForeignKey(Autore, on_delete=models.CASCADE)
    genere = models.ForeignKey(Genere, on_delete=models.CASCADE)
def __str__(self):
        return self.libro_text
class Meta:
    verbose_name_plural = "Libri"


These are the results




Can someone help me?

Team Infinity

unread,
Sep 10, 2019, 12:50:01 PM9/10/19
to django...@googlegroups.com
I think you moved to Django with no prior knowledge of Python.
Here is the fix for that
Screenshot from 2019-09-10 22-31-47.png

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c1941b52-7d2b-4b09-8ead-fce630b73757%40googlegroups.com.

Doddahulugappa.B

unread,
Sep 10, 2019, 2:20:36 PM9/10/19
to django...@googlegroups.com
def __str__(self):
        return self.name



--

Amitesh Sahay

unread,
Sep 10, 2019, 2:50:28 PM9/10/19
to django...@googlegroups.com
I guess you should define the Meta class just under the main class, ie.

class Autore(models.Model):
    nome = models.CharField(max_length=50)
    cognome = models.CharField(max_length=50)
    class Meta:
        verbose_name_plural = "Autori"




Regards,
Amitesh Sahay
91-750 797 8619


Ezequias Rocha

unread,
Sep 11, 2019, 4:36:29 PM9/11/19
to Django users
In your base model write a function like this (if your description field is called 'name'):

    def __str__(self):
        return self.name

It works for me.
PS: Take care of python identation. I don't know if your tabulation is the same you paste here in forum but it placed wrongly could compromise your execution.

Good luck
Ezequias.
Reply all
Reply to author
Forward
0 new messages