用户表:
mysql> describe blog_users;
+--------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| username | varchar(255) | NO | | NULL | |
| slug | varchar(255) | NO | | NULL | |
| password | varchar(300) | NO | | NULL | |
| password_token | varchar(300) | NO | | NULL | |
| email | varchar(255) | NO | | NULL | |
| email_verified | tinyint(1) | NO | | NULL | |
| email_token | varchar(255) | NO | | NULL | |
| email_token_expiry | datetime | NO | | NULL | |
| tos | tinyint(1) | NO | | NULL | |
| active | tinyint(1) | NO | | NULL | |
| last_login | datetime | NO | | NULL | |
| last_action | datetime | NO | | NULL | |
| is_admin | tinyint(1) | NO | | NULL | |
| role | varchar(255) | NO | | NULL | |
| created | datetime | NO | | NULL | |
| modified | datetime | NO | | NULL | |
+--------------------+--------------+------+-----+---------+----------------+
17 rows in set (0.00 sec)
表格:
<form action="/dosave" method="POST">{% csrf_token %}
<label for="id_username">Username:</label>
{{ user.username }}
<label for="id_password">Password:</label>
{{ user.password }}
<label for="id_email">Email:</label>
{{ user.email }}
<input type=submit value="Submit" />
</form>
我只能从输入字段中获取用户名,密码和电子邮件。其他列值将是默认值。如何在保存期间保存这些列的默认值?
模型:
class Users(models.Model):
username = models.CharField(max_length=255)
slug = models.CharField(max_length=255)
password = models.CharField(max_length=300)
password_token = models.CharField(max_length=300)
email = models.CharField(max_length=255)
email_verified = models.BooleanField()
email_token = models.CharField(max_length=255)
email_token_expiry = models.DateTimeField(auto_now_add=True)
tos = models.BooleanField()
active = models.BooleanField()
last_login = models.DateTimeField(auto_now_add=True)
last_action = models.DateTimeField(auto_now_add=True)
is_admin = models.BooleanField()
role = models.CharField(max_length=255)
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return self.username
class UsersModelForm(forms.ModelForm):
class Meta:
model = Users
答案 0 :(得分:2)
答案 1 :(得分:1)
除非我误解了这个问题,否则只需将default
列设置为您希望列默认为的列,并将Null
保留为“否”
答案 2 :(得分:1)
似乎您使用了自定义user model
,因为Django Users
没有slug字段。 (请告诉我,如果我错了)
由于您已创建自己的用户表,因此您需要输入的任何信息都可以在视图中完成。在你的观点中,你可以做这样的事情 if request.method =='POST':
# Extract the POST values
username = request.POST.get('username ', False)
.
.
.
# Create new user object
user = User()
# Fill the POST request information
user.username = username
.
.
# Fill the default values
user.slug = 'What ever you want'
.
.
.
# Save the object
user.save()
但是,如果您希望其他字段也显示在您的表单中,那么正如在其他答案中指出的那样,您可以在模型表单中使用“字段or '
exclude”
class UserForm(ModelForm):
class Meta:
model = User
fields = ('username','password','email','slug')
如果您想要排除几个字段并显示其余字段,那么您可以使用exclude
作为
class UserForm(ModelForm):
class Meta:
model = User
exclude = ('role',)
希望有所帮助