简体   繁体   English

如何在表单中显示多对多字段值?

[英]How can I show a many-to-many field values in a form?

i wrote a code about music and used Many-To-Many-Field() as genre but when i try to show genres it just shows : Genre['a number']我写了一个关于音乐的代码并使用多对多字段()作为流派,但是当我尝试显示流派时,它只显示: Genre['a number']

template:模板:

{% extends 'pages/base.html' %}
    
{% block content %}
    
    <form>
        {% if mdata.image %}
            <img src="mdata.image.url" height="500" width="500">
        {% endif %}
    
        {% for field in form %}
            <p>{{ field.label }} : {{ field.value}}</p>
            }
        {% endfor %}
    </form>
    
    <a href="{% url 'pages:edit_music' mdata.id %}">edit</a>

{% endblock %}

models is here:(i didnt know which one u need so pasted both of them)模型在这里:(我不知道你需要哪一个,所以把它们都粘贴了)

from django.db import models
from django.contrib.auth.models import User


class Genre(models.Model):
    name = models.CharField(unique=True,max_length=20)

    def __str__(self):
        return self.name.title()



class Mdata(models.Model):
    name = models.CharField(max_length=100)
    artist = models.CharField(max_length=150)
    album = models.CharField(max_length=100)
    nation = models.CharField(max_length=100)
    duration = models.DecimalField(max_digits=4,decimal_places=2)
    released_in = models.DecimalField(max_digits=4,decimal_places=0)
    uploaded_at = models.DateTimeField(auto_now_add=True)
    image = models.ImageField(upload_to='images/',blank=True,null=True)
    audio = models.FileField(upload_to='audios/',null=True,blank=True)
    genre = models.ManyToManyField(Genre)
    uploader = models.ForeignKey(User,on_delete=models.CASCADE)

    def __str__(self):
        return self.name.title()+" by "+self.artist.title()

forms.py:(its just a simple form im using) forms.py:(它只是我使用的一个简单的表单)

[![class MdataForm(ModelForm):
class Meta:
    model = Mdata
    fields =\[
        'name',
        'artist',
        'album',
        'genre',
        'nation',
        'duration',
        'released_in',
        'image',
        'audio',
        ]

and here the render :dunno if this is enough [1]: https://i.stack.imgur.com/T5eK2.png在这里渲染:dunno 如果这足够 [1]: https ://i.stack.imgur.com/T5eK2.png

This could be that you don't have a __str__ method on your Genre model.这可能是您的 Genre 模型上没有__str__方法。 Add a __str__ method and return the genre name.添加一个__str__方法并返回流派名称。 That would be something like:那将是这样的:

def __str__(self):
    return self.name

self.name here being the field on your genre model used to specify the name.这里的self.name是您的流派模型上用于指定名称的字段。 In the sample image below, I set the custom user to return the username by default, that way anywhere I call a user it will automatically show the name of the genre.在下面的示例图像中,我将自定义用户设置为默认返回用户名,这样在我调用用户的任何地方它都会自动显示流派的名称。

class CustomUser(AbstractUser):
    id = models.UUIDField(primary_key=True, editable=False, default=uuid4)
    email = models.EmailField(_("email address"), unique=True, null=True)
    phone_number = PhoneNumberField(unique=True, null=True)
    username = models.CharField(max_length=100, unique=True, null=True)
    password = models.CharField(_("password"), max_length=128, null=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        verbose_name = "User"
        verbose_name_plural = "Users"

    def __str__(self):
        return self.username

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

相关问题 Django admin - 如何在自定义管理表单中为多对多字段添加绿色加号 - Django admin - How can I add the green plus sign for Many-to-many Field in custom admin form 如何在表单中显示多对字段对象? - how can i show many-to-mant-field objects in a form? 如何在我的 django HTML 模板中显示多对多字段? - How to show a Many-to-Many field in my django HTML Template? 如何在 Django 管理面板上显示多对多字段? - How to show Many-to-Many Field on Django Admin Panel? 如何从多对多表中检索字段? - How do I retrieve a field from a Many-To-Many table? Django-如何在模型表中选择多对多字段? - Django- how can I get the selections of a many-to-many field in my model's table? 使用 Django 如何获取一组记录,其中用于过滤的值是多对多字段 - Using Django how can I get a set of records where the value used for filtering is to a many-to-many field 如何在Django Restful响应中让不对称的多对多自我关系显示给双方? - How can I get an asymmetric many-to-many self-relationship to show up for both parties in a Django restful response? 如何在Django多对多关系表单中动态显示所选选项? - How to dynamically show selected option in Django Many-to-Many relation form? 保存表单后如何将多对多字段设置为当前用户 - How to set many-to-many field to current user after saving form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM