我创建了 Book
、Book_stat
一个与 Django User
模型相关的 Profile
模型,我正在尝试将Book title
和Profile中的full_name
显示为默认的str返回字符串,来自Book_stat
models.py
from django.db import models
from django.contrib.auth.models import User
from decimal import Decimal
# Create your models here.
class Book(models.Model):
title = models.CharField(max_length=200)
def __str__(self):
return self.title
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
full_name = models.CharField(max_length=150)
def __str__(self):
return self.full_name
class Book_stat(models.Model):
user = models.ForeignKey(User)
book = models.ForeignKey(Book)
rating = models.DecimalField(max_digits=5, decimal_places=2, default=Decimal('0.00'))
like = models.BooleanField(default=False)
def __str__(self):
return self.book.title # here i would like to return book title + full_name from Profile Model
admin.py
from django.contrib import admin
from .models import Book, Book_stat, Profile
# Register your models here.
admin.site.register(Book)
admin.site.register(Book_stat)
admin.site.register(Profile)
当我在 django 管理页面中单击 Book_stat
时,我想在我的 中显示
Book title
和 Profile full_name
作为标题>Book_stat列表
最佳答案
def __str__(self):
return self.book.title +' '+ self.user.profile.full_name
试试这个希望你能得到它
关于Django - 从配置文件模型返回完整名称,该模型与其他模型中的 Django 用户模型相关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45431188/