我创建了一个模型,然后创建了模型表单以接受用户的输入。我正在尝试将作者的默认值设置为活动用户的名称,但是,默认值设置为admin的用户名。请看一下代码。
models.py
from django.db import models
from django.contrib.auth.models import User
from datetime import datetime
class tweets(models.Model):
def curr_user():
for user in User.objects.all():
if user.is_active:
return user.get_username
z = curr_user()
k = datetime.now
author = models.CharField(max_length = 50,default=z)
date_time = models.DateTimeField(default = k)
body = models.TextField()
def __str__(self):
return self.body
forms.py
from django import forms
from .models import tweets
class TweetsForm(forms.ModelForm):
class Meta():
model = tweets
fields = "__all__"
答案 0 :(得分:0)
Check which user is logged (I'm guessing it's the admin) to check this easily go to 'localhost:8000/admin' (only works in local), then logout and login with the user "autor"