D
D
ddgryaz2020-11-27 14:25:53
Django
ddgryaz, 2020-11-27 14:25:53

What did you do wrong when creating models or the model editor?

Hello!

I need to create two models, let's say it's "Directory" and "Directory element". Following Dronov's book "Django 3.0" I bind the models and create an editor in the admin.

Code from the models file:

from django.db import models


class Handbook(models.Model):
    title = models.CharField('Наименование', max_length=250, db_index=True)
    short_title = models.CharField('Короткое наименование', max_length=100)
    description = models.TextField('Описание')
    version = models.CharField('Версия', max_length=250) #unique=True
    date = models.DateField('Дата начала действия справочника этой версии')

    def __str__(self):
        return self.title

    class Meta:
        verbose_name = 'Справочник'
        verbose_name_plural = 'Справочники'


class Element(models.Model):
    r_id = models.ForeignKey(Handbook, on_delete=models.PROTECT, name='Родительский идентификатор')
    code = models.CharField('Код элемента', max_length=250)
    value = models.CharField('Значение элемента', max_length=250)

    def __str__(self):
        return self.value

    class Meta:
        verbose_name = 'Элемент'
        verbose_name_plural = 'Элементы'

Code from admin file:
from django.contrib import admin
from .models import Handbook, Element


class ElementAdmin(admin.ModelAdmin):
    list_display = ('code', 'value', 'r_id')
    list_display_links = ('code', 'value')
    search_fields = ('code', 'value')


admin.site.register(Handbook)
admin.site.register(Element, ElementAdmin)


When running migrations I get an error:
r_id = models.ForeignKey(Handbook.__str__(), on_delete=models.PROTECT, name='Parent ID')
TypeError: __str__() missing 1 required positional argument: 'self'

(venv) D:\PythonProject\DjangoProject\komtek_test \komtek>manage.py makemigrations
SystemCheckError: System check identified some issues:

ERRORS:
: (admin.E108) The value of 'list_display[2]' refers to 'r_id', which is not
a callable, an attribute of 'ElementAdmin' , or an attribute or method on 'handbook.Element'.


Please tell me what am I doing wrong? I need to do this in order to subsequently implement the output on the page of elements related to a particular directory.
Just as I understand it, in the Django admin panel, in the list of all elements, the line "parent identifier" should be displayed - where it will be indicated to which directory a certain element belongs.
In the book of drones, by example, it displays ads related to a specific heading.
Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dr. Bacon, 2020-11-27
@ddgryaz

r_id is not worth naming the field, it is with _id at the end, in the internals this is how id is added to ForeignKey, it is customary to do so handbook = models.ForeignKey(Handbook, ...)Well, I don’t remember that there was a name parameter

A
Art005, 2020-11-27
@Art005

Handbook.__str__().
You call __str__ through the class, respectively, you do not pass an instance of the class (self) to the str method

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question