如何使用表单将登录的用户名保存到数据库中? Django的

时间:2018-06-15 11:15:37

标签: django django-models django-forms django-views

理想情况下保存所有其他数据,但如下所示,用户ID部分显示为下拉栏和空值,该值应为登录用户名。

我的代码出了什么问题? The database page

这是我的代码。

views.py

from .models import Markers
from .forms import AddMarkersInfo
from django.http import HttpResponse

def addinfo(request):
    if request.method == 'POST':
        mks = AddMarkersInfo(request.POST)
        if mks.is_valid():
            submit = mks.save(commit=False)
            submit.user = request.user
            submit.save()

            name = mks.cleaned_data['name']
            address = mks.cleaned_data['address']
            description = mks.cleaned_data['description']
            type = mks.cleaned_data['type']
            lat = mks.cleaned_data['lat']
            lng = mks.cleaned_data['lng']

            Markers.objects.get_or_create(name=name, address=address, description=description,  type=type, lat=lat, lng=lng)
            return render(request, 'home.html', {'mks': mks })
    else:
        mks = AddMarkersInfo()
        return render(request, 'home.html', {'mks': mks})

models.py

from django.db import models
from django.contrib.auth.models import User
from django.conf import settings
from django.contrib.auth import get_user_model

def get_sentinel_user():
    return get_user_model().objects.get_or_create(username='deleted')[0]

class Markers(models.Model):
    User = settings.AUTH_USER_MODEL
    use_id= models.ForeignKey(User, null=True,  on_delete=models.SET(get_sentinel_user),)
    name = models.CharField(max_length=60,default = 'name')
    address = models.CharField(max_length=100,default = 'address')
    description = models.CharField(max_length=150, default='description')
    types = (
        ('m', 'museum'),
        ('s', 'school'),
        ('r', 'restaurant'),
        ('o', 'other'),
    )
    type = models.CharField(max_length=60, choices=types, default='museum')
    lat = models.IntegerField()
    lng = models.IntegerField()

forms.py

from django import forms
from maps.models import Markers

class AddMarkersInfo(forms.ModelForm):
    class Meta:
        model = Markers
        fields = ['name','address','description', 'type','lat','lng',]

1 个答案:

答案 0 :(得分:0)

首先,如果您要使用from django.contrib.auth.models import User,则应删除User = settings.AUTH_USER_MODEL中的行models.pysettings.AUTH_USER_MODEL。你应该只使用其中一个。

您可以将字段更改为:

use_id= models.ForeignKey(settings.AUTH_USER_MODEL, ...

其次,看起来你正在重复创作。行

submit = mks.save(commit=False)
submit.user = request.user
submit.save()

已经创建了Markers个实例,因此无需在此之后调用Markers.objects.get_or_create(...

而且,根据您的模型,该字段应为submit.use_id而不是submit.user

现在,如果我正确理解您的问题,您希望在表单/模板中将use_id字段设为只读。

我不知道为什么该字段会显示在您的表单中,因为它未在您的表单Meta.fields中列出。

您可以尝试设置小部件属性readonly

class AddMarkersInfo(forms.ModelForm):
    class Meta:
        model = Markers
        fields = ['use_id', 'name', 'address', 'description', 'type', 'lat', 'lng']
        widgets = {
            'use_id': forms.Textarea(attrs={'readonly': 'readonly'}),
        }