Something is wrong with a Django model

0

You see, I have created a model which is used to link a comment to a user.

This is the code models.py:

class mensajeria(models.Model):
    texto=RichTextField(max_length=300)
    usuario=models.OneToOneField(User)
    def __srt__(self):
        return self.usuario.username

The administrator should see who the user is, but for some error it does not show what I have in the function __srt__ :

This error is very strange.

Edit: This also affects the shell.

I add: For a moment I managed to make the table show, but now it happens that I get this error: no such table: science_message. It seems to be an error due to the foreign key.

    
asked by Miguel Alparez 26.05.2017 в 13:14
source

1 answer

1

The problem is the method name is __str__ (English String ) and non- __srt__ :

class mensajeria(models.Model):
    texto=RichTextField(max_length=300)
    usuario=models.OneToOneField(User)

    def __str__(self):
        return self.usuario.username

It is recommended to use the decorator python_2_unicode_compatible that is compatible in the versions of Python 2.x and 3.x:

from django.utils.encoding import python_2_unicode_compatible

@python_2_unicode_compatible
class mensajeria(models.Model):
    texto=RichTextField(max_length=300)
    usuario=models.OneToOneField(User)

    def __str__(self):
        return self.usuario.username

What this does it is define methods __unicode__ and __str__ for Python 2 to Python 3 just enough to set the __str__ method as you are doing.

    
answered by 26.05.2017 в 16:50