三行代码  ›  专栏  ›  技术社区  ›  JacksWastedLife

发布到视图不更新数据库

  •  1
  • JacksWastedLife  · 技术社区  · 1 月前

    Project 一个给我 Event .

    class Project(models.Model):
    
        project_name = models.CharField(max_length=50, blank=False, unique=True)
        project_website = models.URLField(max_length=50, blank=True)
        project_description = models.TextField(blank=True)
        ckeditor_classic = models.TextField(blank=True)
        project_category = models.CharField(max_length=15, blank=True)
        project_type = models.CharField(max_length=10, blank=True)
        project_platform = models.CharField(max_length=25, blank=True)
        project_marketcap = models.IntegerField(blank=True, null=True)
        project_stage = models.CharField(max_length=25, blank=True)
        project_blockchain = models.CharField(max_length=25, blank=True)
        project_investors = models.ManyToManyField(Investor, blank=True)
        project_developers = models.ManyToManyField(Developer, blank=True)
    
    
        def __str__(self):
            return str(self.project_name)
    
    

    事件模型

    class Event(models.Model):
    
        project = models.ForeignKey(Project, to_field='project_name', on_delete=models.CASCADE)
        event_name = models.CharField(max_length=100)
        event_date = models.DateTimeField(blank=True)
        event_url = models.URLField(max_length=100, blank=True)
    
        def __str__(self):
            return str(self.event_name)
    
    

    这一事件具有重大意义 ForeignKey

    当我转到某个特定项目的项目页面时,我会单击一个按钮来创建一个事件。

    项目名称是使用此URL预先填充的

    path('apps/add_event/<int:project_id>', view=AddEvent, name="add_event"),

    这创建了一个简单的表单,我可以为事件指定名称和日期,但是当我 POST

    我确实得到了一份工作 在日志中 [25/Nov/2021 21:37:11] "POST /apps/add_event/3 HTTP/1.1" 302 0

    Form.PY

    class addEventForm(forms.ModelForm):
        def __init__(self, *args, **kwargs):
            super(addEventForm, self).__init__(*args, **kwargs)
    
        class Meta:
            model = Event
            fields = '__all__'
    

    View.py

    @login_required
    def AddEvent(request,project_id):
        project = Project.objects.get(pk=project_id)
        form = addEventForm(request.POST or None, instance=project)
        if form.is_valid():
            form.save()
            return redirect('http://127.0.0.1:8000/')
        
        return render(request, 'pages/add_event.html', {'project': project, "form": form})
    

    我想知道是不是这条线 form = addEventForm(request.POST or None, instance=project) instance=event

    我补充说 event = Event.objects.all() 实例=事件 但它抛出了一个错误:

    AttributeError at /apps/add_event/4
    'QuerySet' object has no attribute '_meta'
    

    所以我有点不知所措。有什么想法吗?

    1 回复  |  直到 1 月前
        1
  •  1
  •   Willem Van Onsem    1 月前

    你做不到 project 实例:您的表单在 Event instance=project 您可以让表单编辑属于 event 喜欢 event_name Project 事件名称 .

    因此,这种观点应该是正确的 request.POST .project 事件 以以下形式包装:

    from django.shortcuts import get_object_or_404
    
    @login_required
    def AddEvent(request,project_id):
        project = get_object_or_404(Project, pk=project_id)
        if request.method == 'POST':
            form = addEventForm(request.POST)
            if form.is_valid():
                form.instance.project = project
                form.save()
                return redirect('http://127.0.0.1:8000/')
        else:
            form = addEventForm()
        return render(request, 'pages/add_event.html', {'project': project, "form": form})