[英]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.