简体   繁体   English

Django Model对象创建

[英]Django Model object creation

Look at my django model (I'm pasting here only a part of it): 看一下我的Django模型(我只在其中粘贴一部分):

from django.contrib.auth.models import User as DjangoUser
from django.db import models

class Client(DjangoUser):
    address = models.ForeignKey(Address, blank=True, null=True)

I know how to create fresh Client and User: 我知道如何创建新的客户和用户:

client = Client(username=name, password=pass)
client.save()

This code creates two records: user and client, client references user with its foreign key. 此代码创建两个记录:用户和客户端,客户端使用其外键引用用户。

In my mysql db there is already a DjangoUser record. 在我的mysql数据库中,已经有一个DjangoUser记录。 And now I want to create Client based on this existing user. 现在,我想基于此现有用户创建客户端。 How to do this? 这个怎么做?

User is a special case in the Django framework. User是Django框架中的一种特殊情况。 You should not use inheritance. 您不应该使用继承。

The best practice to add data to it is to create a model and define it as user profile : 向其中添加数据的最佳实践是创建一个模型并将其定义为用户个人资料

Create a model for this: 为此创建一个模型:

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

class UserProfile(models.Model):
    user = models.ForeignKey(User, unique=True) # ensure you create one profile only
    address = models.ForeignKey(Address, blank=True, null=True)

Then you should declare it in the settings as the user profile: 然后,您应该在设置中将其声明为用户个人资料:

AUTH_PROFILE_MODULE = "your_app.UserProfile"

Then in your view: 然后在您看来:

def your_view(request):
    user_profile = request.user.get_profile()
    address = user_profile.address

This is the standard way to do it because Django contrib apps such as the admin, or auth (with login, permissions, etc) will expect the user to be a User class and not the subclass you are creating. 这是执行此操作的标准方法,因为Django contrib应用程序(例如admin或auth(具有登录名,权限等))将期望用户是User类,而不是您正在创建的子类。

If you use inheritance, request.user won't be the object you created and you won't be able to access it's data. 如果使用继承,则request.user不会是您创建的对象,并且您将无法访问其数据。

If your concern is to be able to edit all data concerning the user in the same page, then there is a way to do this : 如果您关心的是能够在同一页面上编辑与用户有关的所有数据,则可以采用以下方法

You could so something like this: 您可以这样做:

from django.contrib.auth.models import User as DjangoUser
from django.db import models

class ClientDetails(models.Model):
   user = models.OneToOneField(DjangoUser)
   address = models.ForeignKey(Address, blank=True, null=True)

And your code to create the object would be something like this: 创建对象的代码如下所示:

#assuming there is a user with this object already, you should add logic to handle the case when there is no user available.
django_user = DjangoUser.objects.get(username=name)

client = Client(user=django_user, password=pass)
client.save()

Or you could do something like this if you want to extend the User, which isn't normally done. 或者,如果您想扩展用户,则可以执行以下操作,而这通常是不行的。 You should use profiles for this. 您应该为此使用配置文件。

from django.contrib.auth.models import User as DjangoUser
from django.db import models

class ClientDetails(DjangoUser):
   address = models.ForeignKey(Address, blank=True, null=True)

And then your client code isn't too much different then you described. 然后,您的客户端代码与您描述的没有太大不同。

#assuming there is a user with this object already, you should add logic to handle the case when there is no user available.
client = Client(username=name, password=pass)
client.save()

User is not abstract, so extending it like that will not work. 用户不是抽象的,所以像这样扩展它是行不通的。 Rather, you should use composition. 相反,您应该使用合成。

from django.contrib.auth.models import User as DjangoUser
from django.db import models

class ClientDetails(models.Model):
   user = models.OneToOneField(DjangoUser)
   address = models.ForeignKey(Address, blank=True, null=True)

This pattern is documented here: http://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users 此模式记录在这里: http : //docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM