Answer the question
In order to leave comments, you need to log in
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 = 'Элементы'
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)
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'.
Answer the question
In order to leave comments, you need to log in
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question