简体   繁体   English

Django:将表单输入保存到 model 时出现溢出错误?

[英]Django: overflow error when saving form input to model?

I'm trying to save input from a simple form to create a new "Post" object.我正在尝试从一个简单的表单中保存输入以创建一个新的“帖子”object。 Models.py:模型.py:

class Post(models.Model):
Author = models.ForeignKey(User, on_delete=models.CASCADE)
Topic = models.ForeignKey(Topic, on_delete=models.CASCADE, blank=True, null=True, 
default=uuid.uuid1, related_name="Topic")
Group = models.ForeignKey(User_Groups, on_delete=models.CASCADE, blank=True, null=True, 
default=uuid.uuid1, related_name="User_Groups")
Content = models.TextField(max_length=100)
Created = models.DateTimeField(default=timezone.now)

def __str__(self): 
    return self.Content  

Views.py:视图.py:

form = PostForm(request.POST)
if request.method == 'POST':
    if form.is_valid():
        new = form.save(commit=False)
        new.Topic = form.cleaned_data.get('Topic')
        new.Content = form.cleaned_data.get('Content')
        new.save()

The code I have now which gives me the error: Python int too large to convert to SQLite INTEGER我现在的代码给了我错误: Python int too large to convert to SQLite INTEGER

Other variations I have tried:我尝试过的其他变体:

form = PostForm(request.POST)
if request.method == 'POST':
    if form.is_valid():
        new = form.save(commit=False)
        new.Topic = form.cleaned_data.get("Topic")
        new.Content = form.cleaned_data.get("Content")
        new.save()

form = PostForm(request.POST)
if request.method == 'POST':
    if form.is_valid():
        new = form.save(commit=False)
        new.Topic = form.cleaned_data('Topic')
        new.Content = form.cleaned_data('Content')
        new.save()

After searching for similar questions, it seems that I may not be correctly creating a new instance of the form, but I am not entirely sure that is the only issue here.在搜索了类似的问题之后,似乎我可能没有正确创建表单的新实例,但我不完全确定这是这里唯一的问题。

I am very new to Django forms, so I apologize for my lack of understanding.我对 Django forms 很陌生,所以我对我的理解不足表示歉意。

Any nudge in the right direction would be greatly appreciated, thanks.任何朝着正确方向的轻推将不胜感激,谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM