gpt4 book ai didi

python - 无法将用户字段内联到 UserProfile 管理选项卡 - 'auth.User' 没有外键到 'MyApp.UserProfile'

转载 作者:行者123 更新时间:2023-12-04 18:00:11 25 4
gpt4 key购买 nike

我正在尝试将用户字段添加到用户配置文件管理选项卡中,以便能够在一个选项卡中更改每个用户属性。问题是 cmd 返回:

编辑:根据商王的评论编辑了代码,但仍然引发错误:

<class 'MyApp.admin.UserProfileUserInline'>: (admin.E202) 'auth.User' has no ForeignKey to 'MyApp.UserProfile'.

事实上,我想在 UserProfile 中内联 User 或在 User 中内联 UserProfile。问题是 UserProfile 有字段 language (注意这不是语言模型),它是 through-model 我不知道如何将它内联到用户(UserProfile 可以毫无问题地内联),所以我正在尝试将 User 内联到 UserProfile。

管理员.py:

from django.contrib import admin
from models import *
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
admin.site.register(AdminContact)

class UserProfileInline(admin.StackedInline):
model = UserProfile
can_delete = False
verbose_name_plural = 'User_Profile'
readonly_fields = ('languages',)

class UserProfileLanguageLevelInline(admin.TabularInline):
model = UserProfileLanguage

class UserAdmin(BaseUserAdmin):
inlines = (UserProfileInline, )

class UserProfileUserInline(admin.StackedInline):
model = User

class UserProfileAdmin(admin.ModelAdmin):
inlines = (UserProfileLanguageLevelInline,User,)

admin.site.unregister(User)
admin.site.register(User,UserAdmin)
admin.site.register(LanguageTuple)
admin.site.register(Language)
admin.site.register(Job)
admin.site.register(UserProfileLanguage)
admin.site.register(Level)
admin.site.register(UserProfile,UserProfileAdmin)

模型.py:

from __future__ import unicode_literals
from django.contrib.auth.models import User
from django.db import models

class AdminContact(models.Model):
email = models.EmailField()
telephone = models.CharField(max_length=40)

def __unicode__(self):
return self.email

class Language(models.Model):
shortcut = models.CharField(max_length=40)
name = models.CharField(max_length=40)

def __str__(self):
return self.name


class LanguageTuple(models.Model):
language_from = models.ForeignKey(Language, related_name='language_from', null=True)
language_to = models.ForeignKey(Language, related_name='language_to', null=True)

def __str__(self):
return '{} to {}'.format(self.language_from, self.language_to)


class Level(models.Model):
LEVEL_CHOICES = (
('unknown','Unknown'),
('standard','Standard level'),
('professional','Professional level'),
('native','Native speaker level'),
)
name = models.CharField(max_length=40,choices=LEVEL_CHOICES, blank=False, null=False)
price_multiplier = models.FloatField()

def __str__(self):
return self.get_name_display()


class UserProfile(models.Model):
user = models.OneToOneField(User, related_name='userprofile')
date_of_birth = models.DateField(null=True,blank=True)
telephone = models.CharField(max_length=40,null=True,blank=True)
IBAN = models.CharField(max_length=40,null=True,blank=True)
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)


MARITAL_STATUS_CHOICES = (
('single', 'Single'),
('married', 'Married'),
('separated', 'Separated'),
('divorced', 'Divorced'),
('widowed', 'Widowed'),
)
marital_status = models.CharField(max_length=40, choices=MARITAL_STATUS_CHOICES, null=True, blank=True)

HOW_DO_YOU_KNOW_ABOUT_US_CHOICES = (
('coincidence', u'It was coincidence'),
('relative_or_friends', 'From my relatives or friends'),
)
how_do_you_know_about_us = models.CharField(max_length=40, choices=HOW_DO_YOU_KNOW_ABOUT_US_CHOICES, null=True,
blank=True)

# TRANSLATOR ATTRIBUTES

is_translator = models.BooleanField(default=False)

# language_tuples = models.ManyToManyField(LanguageTuple,blank=True)
languages = models.ManyToManyField(Language, through='UserProfileLanguage')

rating = models.IntegerField(default=0)

number_of_ratings = models.BigIntegerField(default=0)

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

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

class UserProfileLanguage(models.Model):
userprofile = models.ForeignKey(UserProfile)
language = models.ForeignKey(Language)
level = models.ForeignKey(Level)

class Meta:
unique_together = (('userprofile', 'language'),)


class Job(models.Model):
customer = models.ForeignKey(User, related_name='orders')
translator = models.ForeignKey(User, related_name='jobs',null=True)
price = models.FloatField(null=True,blank=True)

# ZADAVA CUSTOMER
description = models.TextField()
file = models.FileField(null=True,blank=True)
language_tuple = models.ForeignKey(LanguageTuple,related_name='jobs')
specialist = models.BooleanField(blank=True)

created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)

is_active = models.BooleanField(default=False)

is_done = models.BooleanField(default=False)

@property
def has_translator(self):
return self.translator_id is not None


def __str__(self):
return '{}: {}'.format(self.customer,self.language_tuple)

最佳答案

你不应该在 UserProfileAdmininlines 中有 User 模型,因为 User 是一个模型而不是一个内联管理员。像这样编辑它:

class UserProfileAdmin(admin.ModelAdmin):
inlines = (UserProfileLanguageLevelInline, UserProfileUserInline)

关于python - 无法将用户字段内联到 UserProfile 管理选项卡 - 'auth.User' 没有外键到 'MyApp.UserProfile',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36554959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com