将数据添加到相关模型(自定义用户模型)

时间:2017-11-17 15:13:24

标签: python django

我有自定义用户模型。我可以成功地向其添加用户,并希望将关系添加到相关模型。它没有给我任何错误,但没有将用户链接到公司模型中的相关字段压缩器。基本上我在创建用户后需要它,也将用户添加到相关的compusers字段。

这是我在视图中尝试的代码:

os.walk

模型

self.object.compusers.add(self.object.id)

视图

class Company(models.Model):
    user = models.ManyToManyField(settings.AUTH_USER_MODEL,related_name='compusers')
    name = models.CharField(max_length=265, blank=True)
    tel = models.IntegerField(blank=True, null=True)
    email = models.EmailField(max_length=265,blank=True)
    address = models.TextField(blank=True)
    postal = models.TextField(blank=True)
    regno = models.CharField(max_length=265,blank=True)
    vatno = models.CharField(max_length=265,blank=True)

  def __str__(self):
        return self.name

  def get_absolute_url(self):
        return reverse('nodisoapp:home')

表格

class AddStaffView(CreateView):
    form_class = forms.AddStaffMember
    success_url = reverse_lazy('nodisoapp:home')
    template_name = "scrty/addstaff.html"
    def form_valid(self, form):
        self.object = form.save(commit=False)
        self.object.password = get_random_string(length=8)
        self.object.save()
        self.object.compusers.add(self.object.id)
        return super(AddStaffView, self).form_valid(form)

2 个答案:

答案 0 :(得分:0)

我最近经历了同样的过程。它没有最终确定,但应该有所帮助。通过这个项目,我创建了一个用户,该用户(客户)具有所需的明显属性(例如,名字,姓氏,电话,电子邮件,公司和站点)。只是简单地尝试扩展django用户模型并添加运行良好的关系存在很多问题,所以我选择了自己的用户模型。此模型保留使用用户名。我可以提供使用电子邮件而不是用户名的项目。该项目还在登录表单上使用了一些样式小部件。我还有其他项目可以为auth的其他部分设置样式。

主要/ models.py

from django.contrib.auth.models import (
    AbstractBaseUser,
    PermissionsMixin,
    BaseUserManager,
)
from django.core.mail import send_mail
from django.db import models
from django.utils.translation import ugettext_lazy as _
from customers import models as customers_models

class TravelModel(models.Model):
   mileageRate = models.DecimalField(max_digits=4, decimal_places=3)

   def __str__(self):
      return '%s' % (self.mileageRate)

   class Meta:
      verbose_name = "Current Federal Milage Rate"
      #verbose_name_plural = "Milage"


class UserManager(BaseUserManager):
    def create_user(self, username, email, firstName, lastName, company, phone, password=None, **kwargs):
        if not email:
            raise ValueError('Users must have an email address')
        user = self.model(
            username = userName,
            email=self.normalize_email(email),
            firstName = firstName,
            lastName = lastName,
            company = company,
            phone = phone
        )
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, username, email, firstName, lastName, password=None, **kwargs):
        user = self.model(
              username = username,
              email = self.normalize_email(email),
              firstName = firstName,
              lastName = lastName,
        )
        user.set_password(password)
        user.is_superuser = True
        user.is_admin = True
        user.save(using=self._db)
        return user


class MyUser(AbstractBaseUser, PermissionsMixin):
      username = models.CharField(_('Username'), max_length=150, unique=True, null=False, blank=False)
      email = models.EmailField(_('E-mail Address'), max_length=255, unique=True)
      firstName = models.CharField(_('First Name'),max_length=50, blank=False, null=False)
      lastName = models.CharField(_('Last Name'),max_length=50, blank=False, null=False)
      company = models.ForeignKey(customers_models.CompanyModel, on_delete=models.PROTECT, null=False)
      phone = models.ForeignKey(customers_models.PhoneModel, on_delete=models.PROTECT, null=False)
      is_admin = models.BooleanField(
            _('staff status'),
            default=False,
            help_text=_(
                  'Designates whether the user can log into this admin '
                  'site.'
            )
      )
      is_active = models.BooleanField(
            _('active'),
            default=True,
            help_text=_(
                  'Designates whether this user should be treated as '
                  'active. Unselect this instead of deleting accounts.'
            )
      )

      objects = UserManager()

      USERNAME_FIELD = 'username'
      REQUIRED_FIELDS = ['email','firstName', 'lastName']

      class Meta(object):
            ordering = ['firstName']
            verbose_name = _('Contact')
            verbose_name_plural = _('Contacts')

      def __str__(self):
            return '%s - %s %s - %s - %s' % (self.company, self.firstName, self.lastName, self.email, self.phone)

      def get_full_name(self):
            return self.email

      def get_short_name(self):

      def has_perm(self, perm, obj=None):
            return True

      def has_module_perms(self, app_label):
            return True

      def email_user(self, subject, message, from_email=None, **kwargs):
            send_mail(subject, message, from_email, [self.email], **kwargs)
      @property
      def is_staff(self):
            return self.is_admin

主要/ views.py

from django.shortcuts import render, redirect
from django.http import HttpResponseRedirect
from django.contrib import messages
from django.contrib.auth import logout, login, authenticate, update_session_auth_hash
from django.contrib.auth.decorators import login_required, permission_required
from django.contrib.auth.forms import PasswordChangeForm
from django.core.mail import send_mail
from .models import MyUser
from .forms import RegistrationForm, LoginForm, ResetPasswordForm


def IndexView(request):
    return render(request, 'main/index.html')


def ContactView(request):
    return render(request, 'main/contact.html')


def LoginView(request):
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            return redirect('home')
    else:
        form = LoginForm()
        args={ 'form': form }
    return render(request, 'main/login.html', args)


def LogoutView(request):
    logout(request)

@login_required
def PasswordChangeView(request):
    if request.method == 'POST':
        form = PasswordChangeForm(request.user, request.POST)
        if form.is_valid():
            user = form.save()
            update_session_auth_hash(request, form.user)  # Important!
            messages.success(request, 'Your password was successfully updated!')
            return redirect('change_password_done')
        else:
            messages.error(request, 'Please correct the error below.')
    else:
        form = PasswordChangeForm(request.user)
    return render(request, 'main/change_password_form.html', {
        'form': form
    })


@permission_required(['auth.add_customuser', 'auth.change_customuser'], raise_exception=True)
def RegisterView(request):
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            form.save()
        return redirect('main')
    else:
        form = RegistrationForm()

    args = {'form': form}
    return render(request, 'main/register.html', args)

主要/ urls.py

from django.conf.urls import url, include
from django.urls import reverse_lazy
from .views import IndexView, RegisterView, ContactView, PasswordChangeView
from .forms import LoginForm
from django.contrib.auth.views import (
    LogoutView,
    login,
    PasswordChangeView,
    PasswordChangeDoneView,
    PasswordResetView,
    PasswordResetDoneView,
    PasswordResetConfirmView,
    PasswordResetCompleteView,
)

# main app URLS extend from the base url i.e. www.website.com/*

urlpatterns = [
    url(r'^$', IndexView, name='main'),
    url(r'^register/$', RegisterView, name='register'),
    url(r'^contact/$', ContactView, name='contact'),
    url(r'^logout/$', LogoutView.as_view(next_page=reverse_lazy('main')),
    name='logout'
    ),
        url(r'^service/', include('service.urls')),
        url(r'^consultation/', include('consultation.urls')),
    # successful logins are redirected to the settings.LOGIN_REDIRECT_URL
    url(r'^login/$', login, name='login', kwargs={'template_name':'main/login.html', 'authentication_form':LoginForm},
        ),
    url(r'^change_password/$',
        PasswordChangeView.as_view(
            template_name='main/change_password_form.html'
        ),
        name='password_change'),
    url(r'^change_password/done/$',
        PasswordChangeDoneView.as_view(
        template_name='main/change_password_done.html'
        ),
        name='password_change_done'),
    url(r'^password_reset/$',
        PasswordResetView.as_view(template_name='main/reset_password_form.html'),
        name='password_reset'),
    url(r'^password_reset/done/$',
        PasswordResetDoneView.as_view(template_name='main/reset_password_done.html'),
        name='password_reset_done'),
    url(r'^reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
        PasswordResetConfirmView.as_view(template_name='main/reset_password_confirm.html'),
        name='password_reset_confirm'),
    url(r'^reset/done/$',
        PasswordResetCompleteView.as_view(template_name='main/reset_password_complete.html'),
        name='password_reset_complete'),
]

主要/ forms.py

   from django import forms
    from django.contrib.auth.forms import (
        AuthenticationForm,
        UserCreationForm,
        UserChangeForm,
        PasswordResetForm,
    )
    from .models import MyUser

    class ResetPasswordForm(PasswordResetForm):
        pass

    class LoginForm(AuthenticationForm):
        username = forms.CharField(label='Username',
                                   widget=forms.TextInput(attrs={'placeholder':'Username'}))
        password = forms.CharField(label='Password',
                                   widget=forms.PasswordInput(attrs={'placeholder':'Password'}))

    class RegistrationForm(UserCreationForm):
        email = forms.EmailField(required=True)

        class Meta:
            model = MyUser
            fields = (
                'username',
                'firstName',
                'lastName',
                'email',
                'password1',
                'password2'
            )

        def save(self, commit=True):
            user = super(RegistrationForm, self).save(commit=False)
            user.first_name = self.cleaned_data['firstName']
            user.last_name = self.cleaned_data['lastName']
            user.email = self.cleaned_data['email']

            if commit:
                user.save()

            return user

<强>客户/ models.py

from django.db import models
from django.core.validators import RegexValidator
from main import models as main_models
from django.utils.translation import ugettext_lazy as _


class PhoneModel(models.Model):
    phone_regex = RegexValidator(regex=r'^\+?1?\d{9,15}$',
                                 message="Phone number must be entered in the format: '+999999999'. Up to 15 digits allowed."
                                )
    phone_number = models.CharField(_('Phone Number'),validators=[phone_regex],
                                    max_length=15, blank=True
                                   ) # validators should be a list

    def __str__(self):
        return '%s' % (self.phone_number)

    class Meta:
        verbose_name = "Phone Number"
        #verbose_name_plural = "Phone Numbers"


class CompanyModel(models.Model):
    name = models.CharField(_('Company Name'),max_length=255)
    since = models.DateField(auto_now_add=True)

    def __str__(self):
        return '%s - Customer since %s' % (self.name, self.since)

    class Meta:
        ordering = ['name']
        verbose_name = "Company Name"
        #verbose_name_plural = "Companies"


class DetailsModel(models.Model):
    company = models.OneToOneField(CompanyModel, on_delete=models.CASCADE, primary_key=True)
    distance = models.SmallIntegerField(blank=True)
    rate = models.DecimalField(max_digits=5, decimal_places=2)

    def __str__(self):
        return '%s - Distance: %s - Rate: %s' % (self.company, self.distance, self.rate)

    class Meta:
        ordering = ['company']
        verbose_name = "Customer Agreement Information"
        #verbose_name_plural = "Details"


class SiteModel(models.Model):
    company = models.ForeignKey(CompanyModel, on_delete=models.CASCADE)
    #contact = models.ForeignKey(main_models.User.email, on_delete=models.CASCADE)
    details = models.ForeignKey(DetailsModel, on_delete=models.CASCADE)
    street1 = models.CharField(max_length=255)
    street2 = models.CharField(max_length=255, blank=True)
    city = models.CharField(max_length=50)
    state = models.CharField(max_length=2)
    zipcode = models.IntegerField()
    country = models.CharField(max_length=50)

    def __str__(self):
        return '%s - %s, %s' % (self.company, self.city, self.state)

    class Meta:
        ordering = ['company']
        verbose_name = "Customer Site Information"
        #verbose_name_plural = "Sites"


class ContactModel(models.Model):
    company = models.ForeignKey(CompanyModel, on_delete=models.PROTECT, null=True)
    first_name = models.CharField(_('First Name'),max_length=255)
    last_name = models.CharField(_('First Name'),max_length=255)
    email = models.EmailField(_('E-mail Address'), max_length=255, unique=True)
    phone = models.ForeignKey(PhoneModel, on_delete=models.PROTECT, null=True)

    def __str__(self):
        return '%s %s of %s' % (self.first_name, self.last_name, self.company)

答案 1 :(得分:0)

我找到答案,多对多字段需要公司参考,因此您只需添加公司实例而不是添加用户实例