Use of def __unicode __ (self):

3

Use Django 1.9. I have two models related to each other, one contains an id and name among other attributes and the other contains its own id along with the id of the first model:

class Televisor(models.Model):
    idTelevisor = models.AutoField(primary_key=True)
    marca = models.CharField(max_length=45, null=False)
    modelo = models.CharField(max_length=45, null=False)
    ...

class Falla(model.Models):
    idFalla = models.AutoField(primary_key=True)
    idTelevisor = ForeignKey(Televisor)
    ...

However, in the admin of django I would like for the second model, the names of the objects to come out using the attributes contained in the first model. Which I have been unable to do with the syntax that I will describe later, since I get an ObjectHasNoAttribute error.

In that case, what is the syntax? For example, I have this syntax, but it causes errors because the attributes do not exist in the second model:

def __unicode__(self):
    return '{} {} {}'.format(self.televisor.Marca, self.televisor.Modelo, self.televisor.VarianteModelo)

Greetings and thanks in advance

    
asked by KPavezC 10.02.2016 в 02:44
source

3 answers

2

In the end it was not necessary to describe the attributes that I wanted to show as a name in Django's admin, it was enough to put the id that was the ForeignKey:

class Falla(models.Model):
   ...

    def __unicode__(self):
      return '{}'.format(self.idTelevisor)
    
answered by 10.02.2016 в 04:05
1

I do not understand very well the id, but you could relate the models in the following way, given as an example:

class Author(models.Model):
    first_name = models.CharField(max_length=30)
    last_name  = models.CharField(max_length=30)

    def __unicode__():
        return '{} {}'.format(self.first_name, self.last_name)


class Gender(models.Model):
    author = models.ForeignKey(Author)
    book  = models.CharField(max_length=30)

    def __unicode__():
         return '{}'.format(self.book)

Then modify the admin.py module to add to your administration interface.

from django.contrib import admin
from .models import Register, User, Author, Gender
# Register your models here.

admin.site.register(Register)
admin.site.register(User)
admin.site.register(Author)
admin.site.register(Gender)

I think that with that you can relate model 1 to model 2, and you can verify in your admin interface, how the relationship is created by filling in the fields.

    
answered by 10.02.2016 в 03:36
1

You do not really need to define your primary key that way, since Django does exactly that automatically. That is, you can skip the first field:

class Televisor(models.Model):
    marca = models.CharField(max_length=45, null=False)
    modelo = models.CharField(max_length=45, null=False)
    ...

On the other hand, the error that you mention is due to the fact that in your model you do not have any field called televisor . The correct name, as you have already discovered, is idTelevisor .

Finally, the Django style guide recommended that you use only lowercase in the names of your fields, so the model Falla would look like this:

class Falla(model.Models):
    televisor = ForeignKey(Televisor)
    ...
    
answered by 10.02.2016 в 10:11