我有一个表单,当用户提交表单时,我需要当前登录的用户自动在django管理员中填充。我希望当前用户的用户名填充在django管理员中显示User
的列中。截图。
如何使用当前代码正确执行该操作?
如果有帮助,我正在使用的自定义用户模型位于from users.models import CustomUser
中。
我很感激任何帮助,干杯
from django.contrib import admin
from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAdmin
from user_profile.forms import HomeForm
from users.forms import CustomUserCreationForm, CustomUserChangeForm
from user_profile.models import Listing
from users.models import CustomUser
# Register models here.
class UserProfileAdmin(admin.ModelAdmin):
list_display = ['name', 'address', 'zip_code', 'mobile_number', 'created', 'updated', 'get_username']
list_filter = ['name', 'zip_code', 'created', 'updated',]
def get_username(self, obj):
if obj.user is not None:
return obj.user.username
return '-'
get_username.short_description = 'User'
admin.site.register(Listing, UserProfileAdmin)
#user_profile/models
from django.contrib import auth
from django.db import models
from django.urls import reverse
from django.contrib.auth.models import AbstractUser, UserManager
from django.contrib.auth.models import BaseUserManager
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.conf import settings
from users.forms import CustomUserCreationForm, CustomUserChangeForm
from users.models import CustomUser
class Listing (models.Model):
# user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True)
user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, on_delete=models.CASCADE)
created = models.DateTimeField(auto_now_add=True, null=True)
updated = models.DateTimeField(auto_now=True)
name = models.CharField(max_length=100)
address = models.CharField(max_length=100)
zip_code = models.CharField(max_length=100)
mobile_number = models.CharField(max_length=100)
# cc_number = models.CharField(max_length=100)
# cc_expiration = models.CharField(max_length=100)
# cc_cvv = models.CharField(max_length=100)
# objects = ListingManager()
def get_username(self):
return self.user.username
def create_profile(sender, **kwargs):
if kwargs['created']:
user_profile = Listing.objects.create(user=kwargs['instance'])
post_save.connect(create_profile, sender=CustomUser)
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render, redirect
from django.conf import settings
from django.views.generic.edit import FormView
from django.views.generic.edit import UpdateView
from django.views.generic import TemplateView
from .forms import HomeForm
from users.forms import CustomUserCreationForm, CustomUserChangeForm
from .models import Listing
from users.models import CustomUser
from django.urls import reverse_lazy
from django.utils import six
from django.utils.translation import ugettext as _
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from avatar.forms import PrimaryAvatarForm, DeleteAvatarForm, UploadAvatarForm
from avatar.models import Avatar
from avatar.signals import avatar_updated, avatar_deleted
from avatar.utils import (get_primary_avatar, get_default_avatar_url,
invalidate_cache)
def change_view(request):
form = HomeForm(request.POST or None)
user_profile = Listing.objects.all
user = request.user
if form.is_valid():
form.save()
form = HomeForm()
context = {
'form': form, 'user_profile': user_profile
}
return render(request, "myaccount.html", context)
import os
from django import forms
from django.forms import ModelForm
from django.forms import widgets
from django.utils import six
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _
from django.template.defaultfilters import filesizeformat
from avatar.conf import settings
from avatar.models import Avatar
from .models import Listing
class HomeForm(forms.ModelForm):
# user = forms.CharField(required=False, label='', max_length=100, widget=forms.TextInput(attrs={'placeholder': 'CVV', 'class': 'form-control'}))
created = forms.CharField(required=False, label='', max_length=100, widget=forms.TextInput(attrs={'placeholder': 'CVV', 'class': 'form-control'}))
name = forms.CharField(required=False, label='', max_length=100, widget=forms.TextInput(attrs={'placeholder': 'Full Name', 'class': 'form-control'}))
address = forms.CharField(required=False, label='', max_length=100, widget=forms.TextInput(attrs={'placeholder': 'Address', 'class': 'form-control'}))
zip_code = forms.CharField(required=False, label='', max_length=100, widget=forms.TextInput(attrs={'placeholder': 'Zipcode', 'class': 'form-control'}))
mobile_number = forms.CharField(required=False, label='', max_length=100, widget=forms.TextInput(attrs={'placeholder': 'Mobile Number', 'class': 'form-control'}))
cc_number = forms.CharField(required=False, label='', max_length=100, widget=forms.TextInput(attrs={'placeholder': 'Credit Card', 'class': 'form-control'}))
cc_expiration = forms.CharField(required=False, label='', max_length=100, widget=forms.TextInput(attrs={'placeholder': 'Expiration Date', 'class': 'form-control'}))
cc_cvv = forms.CharField(required=False, label='', max_length=100, widget=forms.TextInput(attrs={'placeholder': 'CVV', 'class': 'form-control'}))
class Meta:
model = Listing
fields = ['name', 'address', 'zip_code', 'mobile_number']
<form role="form" action="" method="post">
{% csrf_token %}
{{ form.errors }}
{{ form.name }}
{{ form.address }}
{{ form.zip_code }}
{{ form.mobile_number }}
<button class="btn btn-primary btn-success btn-round btn-extend" type="submit" value="Submit"><i class="zmdi zmdi-favorite-outline6"></i>Submit</button>
</form>
AUTH_USER_MODEL = 'users.CustomUser'
答案 0 :(得分:2)
免责声明:我不确定您的代码是否正确。如果一切正常,也可以:)
def change_view(request):
form = HomeForm(request.POST or None)
user_profile = Listing.objects.all
user = request.user
if form.is_valid():
listing_instance = form.save() # "this will return the 'Listing' instance"
listing_instance.user = user # assign 'user' instance
listing_instance.save() # calling 'save()' method of model
form = HomeForm()
context = {
'form': form, 'user_profile': user_profile
}
return render(request, "myaccount.html", context)
答案 1 :(得分:0)
我认为您只需要dataSrc: function (json) {
var jsonData = JSON.parse(json);
return jsonData.data;
}
的{{1}}用户模型即可。
喜欢..
register
希望有帮助。