【问题标题】:Django how to remove the ForeignKey in the templateDjango如何删除模板中的ForeignKey
【发布时间】:2021-04-24 12:00:37
【问题描述】:

我想在表单和模板中删除 ForeignKey,它会自动生成它在此处输入代码

模型和表格在那里:

from django.db import models
from django.contrib.auth.models import User
from django.forms import ModelForm
class PostProduit(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    titre = models.CharField(max_length=255)
    categorie = models.CharField(max_length=255)
    description = models.TextField(max_length=255)
    prix_en_FCFA = models.CharField(max_length=255)
    Whatsapp = models.BooleanField(max_length=255)
    photo_1 = models.ImageField(upload_to="image/")
    photo_1 = models.ImageField(upload_to="image/", null=True, blank=True)
    photo_1 = models.ImageField(upload_to="image/", null=True, blank=True)
    photo_1 = models.ImageField(upload_to="image/", null=True, blank=True)
    date = models.DateTimeField(auto_now=True)
    def __str__(self):
        return f"{self.user} ===> {self.titre}"

class PostProduitForm(ModelForm):
    class Meta:
        model = PostProduit
        fields = '__all__'

【问题讨论】:

    标签: django


    【解决方案1】:

    只需在 fields 变量中添加要在表单中添加的字段,如下所示:

    fields = ['titre', 'categorie', 'description', ...]
    

    您可以随时参考Official Django documentation 了解更多信息。

    【讨论】:

    • 但是如何在不让用户看到的情况下在 html 文件中添加自动 ForeignKey
    • 我不太明白你的意思。你的意思是你想在模板中访问它而不在表单中?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-14
    • 2011-05-30
    • 2021-09-15
    • 2013-09-22
    • 2018-02-11
    • 2012-06-02
    • 2016-02-03
    相关资源
    最近更新 更多