将ForeignKey字段添加到默认用户而不进行修改?

时间:2017-11-04 00:43:42

标签: django django-models

我正在使用默认的用户模型,并创建了一个名为Team的自定义模型。用户只能在一个团队中,但团队可以拥有多个用户。因此,我必须在User模型中创建ForeignKey字段。

问题是,我只是使用NTSTATUS CheckASLR(HANDLE hProcess, PVOID hmod, BOOLEAN& bASLR) { static volatile UCHAR guz = 0; PVOID stack = alloca(guz); SIZE_T cb = 0, rcb = MAX_PATH*sizeof(WCHAR); union { PVOID buf; PUNICODE_STRING ObjectName; }; NTSTATUS status; do { if (cb < rcb) { cb = RtlPointerToOffset(buf = alloca(rcb - cb), stack); } if (0 <= (status = NtQueryVirtualMemory(hProcess, hmod, MemoryMappedFilenameInformation, buf, cb, &rcb))) { HANDLE hFile, hSection; IO_STATUS_BLOCK iosb; OBJECT_ATTRIBUTES oa = { sizeof(oa), 0, ObjectName }; status = NtOpenFile(&hFile, FILE_GENERIC_READ, &oa, &iosb, FILE_SHARE_VALID_FLAGS, FILE_SYNCHRONOUS_IO_NONALERT); if (0 <= status) { status = NtCreateSection(&hSection, SECTION_QUERY, 0, 0, PAGE_READONLY, SEC_IMAGE, hFile); NtClose(hFile); if (0 <= status) { SECTION_IMAGE_INFORMATION sii; status = ZwQuerySection(hSection, SectionImageInformation, &sii, sizeof(sii), 0); NtClose(hSection); if (0 <= status) { bASLR = sii.ImageDynamicallyRelocated; } } } break; } } while (status == STATUS_BUFFER_OVERFLOW ); return status; } 导入用户

来使用默认用户

将ForeignKey字段添加到默认用户模型的最简单方法是什么?我是否必须扩展默认的用户模型?

或者有没有办法让我在Team中添加一个ForeignKey字段,并交换用户和团队之间的关系?

2 个答案:

答案 0 :(得分:0)

老实说,当您使用默认的Django用户模型时,创建一个可以更容易修改的自定义用户模型总是更好。下面是一个关于如何使用AbtractBaseUser修改User类的简单示例。如果您想添加外键,只需使用ForeignKey而不是下面的数据类型。

from __future__ import unicode_literals
from django.utils import timezone
from django.contrib.auth.models import (AbstractBaseUser,PermissionsMixin)
from django.db import models

class User(AbstractBaseUser, PermissionsMixin):

    email = models.EmailField(unique=True)
    username = models.CharField(max_length=7, unique=True)
    formattedusername = models.CharField(max_length=11, unique=True, primary_key = True)
    first_name = models.CharField(max_length=40)
    last_name = models.CharField(max_length=140)
    date_joined = models.DateTimeField(default=timezone.now)
    is_active = models.BooleanField(default=True)
    is_staff = models.BooleanField(default=False)
    facility = models.CharField(max_length=140)
    jobdescription = models.CharField(max_length=140)
    positiondescription = models.CharField(max_length=140)
    coid = models.CharField(max_length=5)
    streetaddress = models.CharField(max_length=140)



    USERNAME_FIELD = 'username'

    class Meta:
        app_label = 'accounts'
        db_table = "user"

#    REQUIRED_FIELDS = "username"

    def __str__(self):
        return "@{}".format(self.username)

答案 1 :(得分:0)

在我看来,实现目标的最佳方法是使用中间表。因此,您可以照常创建Team模型,然后使用两个外键创建另一个模型UserTeam。这是一个例子,它可能会改变,随意根据您的需要进行更改。

class Group(models.Model):
   group = models.ForeignKey(User, on_delete=models.CASCADE)
   group = models.ForeignKey(Team, on_delete=models.CASCADE)


class Team (models.Model):
   group = models.ForeignKey(Group, on_delete=models.CASCADE)
   #your other fields