在 Django Admin 中添加/编辑内联外键字段时出现问题
我有两个模型,School 和 UserProfiles,其中 UserProfiles 有一个指向 School 的外键。在 Django 管理中,我希望能够选择一所学校并让它显示所有具有外键的用户配置文件。
我这样做的方法是创建一个新的内联,模型是 UserProfile,并将其添加到新的 SchoolAdmin 并注册它。
当我去添加新的用户配置文件时,我的问题出现了。发生的情况是,我从下拉框中选择一个现有用户,然后单击“保存”。但是,我收到一条错误消息,指出“该用户的用户配置文件已存在”。
似乎在保存后,它会尝试为我选择的用户创建一个新的用户配置文件。我会以错误的方式这样做吗?
这是我设置的代码,用于在创建用户时自动创建用户配置文件。
def create_user_profile(sender, instance, created, **kwargs):
"""Create the UserProfile when a new User is saved"""
print "User Profile Creation: False"
if created:
print "User Profile Creation: ", created
UserProfile.objects.get_or_create(user=instance)
post_save.connect(create_user_profile, sender=User)
编辑:作为旁注,我如何才能跟踪程序流程以进行调试?我发现仅处理打印语句很困难。
I have two models, School and UserProfiles, where UserProfiles has a ForeignKey to School. In the Django Admin, I want to be able to pick a school and have it display all the userprofiles that have a foreign key to it.
The way that I did this was to create a new Inline, with the model being the UserProfile, and adding it to the new SchoolAdmin and registering it.
My problem arises when I go to add a new userprofile. What happens is that I pick an existing user from the dropdown box and click save. However, I then get an error stating that a "User Profile with this User already exists."
It seems that upon saving, it tries to create a new User Profile for the user I pick. Am I going about doing this the wrong way?
Here is the code that I have set up to auto-create the user profiles when a user is created.
def create_user_profile(sender, instance, created, **kwargs):
"""Create the UserProfile when a new User is saved"""
print "User Profile Creation: False"
if created:
print "User Profile Creation: ", created
UserProfile.objects.get_or_create(user=instance)
post_save.connect(create_user_profile, sender=User)
EDIT: As a side-note, how can I go about tracing the program flow for debugging? I find it difficult to deal with just print statements.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定你设置错了什么。但是,错误消息应该是线索。在某个时候,您已经为用户 X 创建了一个用户配置文件,然后您尝试创建另一个。需要注意的是,django 不会自动为用户创建配置文件,因此这必须是您编写的代码。
如果没有看到代码,我只是在黑暗中拍摄。请记住,您可以通过多种方式创建这些配置文件,例如您可以在用户模型上注册 post_save 信号。
至于追踪:
http://docs.python.org/library/pdb.html
或者一些很酷的在崩溃调试之后,您可以尝试 http://packages.python.org/django-extensions/ 与它的实现werkzeug 调试器在 http://packages.python.org/django-extensions/runserver_plus.html 中构建
I'm not sure what you have set up wrong. However, the error message should be the clue. At some point you've created a userprofile for User X and then you've tried to create another. It's important to note that django does not autocreate profiles for users so this has to be code that you've written.
Without seeing code I'm just shooting in the dark here though. Keep in mind their are multiple ways you could be creating these profiles such as perhaps you registered the post_save signal on the user model.
As for tracing:
http://docs.python.org/library/pdb.html
Or for some cool after the fact crash debugging you could try http://packages.python.org/django-extensions/ with it's implementation of the werkzeug debugger build in http://packages.python.org/django-extensions/runserver_plus.html