Django管理员即使没有丢失也会丢失字段

编程入门 行业动态 更新时间:2024-10-26 20:32:10
本文介绍了Django管理员即使没有丢失也会丢失字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述
  • Python 2.7
  • Django 1.3

当我在admin.py中包含'user_id','user',然后...

When I include 'user_id','user' in the admin.py, then...

  • 如果我无论如何提交,那么它将显示一个这个字段的用户字段需要。错误消息。
  • 如果我选择一个用户&再次提交,然后我得到TimeslipAdmin.fields'表示从表单中缺少的字段'user_id'。即使'user_id'在我的admin.py (见下文)
  • no user field shows up in the form when I click to add a Timeslip.
  • If I submit it anyway, then it shows the user field with a "This field is required." error message.
  • If I pick a user & submit again, then I get "'TimeslipAdmin.fields' refers to field 'user_id' that is missing from the form." even though 'user_id' is clearly listed in my admin.py (see below)

Traceback说 -

The Traceback says --

  • 异常类型:在/ admin / timeslip / timeslip / add /
  • 中不正确地配置
  • 异常值:'TimeslipAdmin.fields'是指缺少的字段'user_id'表单。
  • Exception Type: ImproperlyConfigured at /admin/timeslip/timeslip/add/
  • Exception Value: 'TimeslipAdmin.fields' refers to field 'user_id' that is missing from the form.

但是...如果我离开'user_id','user' out of admin.py then ....

But...if I leave 'user_id','user' out of the admin.py then....

  • 当我点击添加Timeslip时,没有用户字段显示。 / li>
  • 无论如何,提交它,它显示用户字段& 此用户的Timeslip已存在。错误消息。 (这不应该是一个错误,因为我希望用户有多个Timeslip的意思是另一个错误,我必须弄清楚,一旦我可以得到这种形式工作)
  • no user field shows up when I click to add a Timeslip.
  • Submit it anyway, and it shows the user field & a "Timeslip with this User already exists." error message. (which shouldn't be an error either 'cause I want users to have multiple Timeslip's which means another error I'll have to figure out once I can just get this form working)

admin.py

from timeslip.models import Timeslip from django.contrib import admin class TimeslipAdmin(admin.ModelAdmin): fields = ['user_id','user','day','hours_as_sec','part_of_day','drove','gas_money','notes'] admin.site.register(Timeslip, TimeslipAdmin)

models.py

from django.db import models from django.contrib.auth.models import User # Create your models here. class Timeslip(models.Model): user=models.ForeignKey(User) day = models.DateField() hours_as_sec = models.PositiveIntegerField() part_of_day = models.CharField(max_length=16,choices=PART_O_DAY) drove = models.BooleanField(default=False) gas_money = models.DecimalField(max_digits=5,decimal_places=2) notes = models.TextField() class UserProfile(models.Model): user = models.ForeignKey(User) url = models.URLField("Website", blank=True) position = models.CharField(max_length=50, blank=True) User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])

我非常无知如何克服这个。我来自PHP背景,一个新手到Python& Django。

I'm very clueless how to overcome this. I'm coming from a PHP background, a newbie to Python & Django.

推荐答案

user_id 和 / code>是多余的。 Django自动在数据库中自动命名您的用户字段 user_id (因为它保存了它指向的User实例的ID。 )

user_id and user are redundant. Django automatically names your user field user_id in the database (since it holds the id for the User instance it points to.)

将您的admin.py更改为以下内容,它应该可以工作:

Change your admin.py to the following and it should work:

class TimeslipAdmin(admin.ModelAdmin): fields = ['user','day','hours_as_sec','part_of_day','drove','gas_money','notes']

此外,您还包括管理员中的所有字段,因此您确实不需要指定字段。这样做也可以:

Also you're including all of the fields in the admin, so you really don't need to specify the fields. This would work just as well:

class TimeslipAdmin(admin.ModelAdmin): pass

更多推荐

Django管理员即使没有丢失也会丢失字段

本文发布于:2023-11-25 18:33:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630872.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:也会   字段   管理员   Django

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!