Django Form,如何获取ModelChoiceField的更多模型信息

edqdpe6u  于 2023-05-01  发布在  Go
关注(0)|答案(1)|浏览(113)

我有三个模型,它们彼此相关。放映中有“film”和“screen”外键,它们链接到Film和Screen。我正在尝试设置一个网页,以便您可以查看已设置的放映信息,并从可用的电影和屏幕的下拉列表中编辑它。
我现在遇到的问题是,我的下拉菜单显示的不是屏幕编号和可供选择的电影标题,而是“屏幕对象(4)”或“电影对象(2)”。如何从每个对象中获取要显示下一级信息?
我的模特是:

class Film(models.Model):
    ageRatings =[
        ("U", "U"),
        ("PG", "PG"),
        ("12", "12"),
        ("15", "15"),
        ("18", "18"),
    ]

    id = models.AutoField(primary_key=True)
    title = models.CharField(max_length=255)
    length = models.DurationField()
    rating = models.CharField(max_length=3, choices=ageRatings, default="U")
    genre = models.CharField(max_length=50)
    poster_url = models.CharField(max_length=255, default='')
    
    
class Screen(models.Model):
    id = models.AutoField(primary_key=True)
    cinema = models.ForeignKey(Cinema, on_delete=models.CASCADE, related_name='screens')
    screen_number = models.PositiveSmallIntegerField()
    seating_capacity = models.PositiveIntegerField()
    

class Showing(models.Model):
    id = models.AutoField(primary_key=True)
    film = models.ForeignKey(Film, on_delete=models.CASCADE, related_name='showings')
    screen = models.ForeignKey(Screen, on_delete=models.CASCADE, related_name='showings')
    start_time = models.DateTimeField()
    end_time = models.DateTimeField(null=True)
    available_seats = models.IntegerField()
    adult_price = models.FloatField()
    student_price = models.FloatField()
    child_price = models.FloatField()
    
    def save(self, *args, **kwargs):
        self.end_time = self.start_time + self.film.length
        super().save(*args, **kwargs)

    def __str__(self):
        out = self.start_time.strftime("%d %B %Y %I:%M%p")
        out = out + " - " + self.film.title
        print(out)
        return out

我的表格是:

class ShowingForm(forms.ModelForm):
    screen = forms.ModelChoiceField(queryset=Screen.objects.all().order_by('screen_number'))
    film = forms.ModelChoiceField(queryset=Film.objects.all().order_by('title'))
    class Meta:
        model = Showing
        fields = ['screen', 'film', 'start_time']

我的HTML是:

{% extends 'base.html' %}

{% block content %}

<div class="container mt-4">
  <h1>Update Showing</h1>
  <form method="post" action="{% url 'update_showing' showing.id %}">
    {% csrf_token %}
    {% for field in form %}
      <div class="form-group">
        {{ field.label_tag }}
        {{ field }}
      </div>
    {% endfor %}
    <button type="submit" class="btn btn-primary">Update Showing</button>
  </form>
</div>

{% endblock %}
sbdsn5lh

sbdsn5lh1#

Django搜索ScreenFilm的字符串表示。您必须用与Showing相同的方法来定义它,实现__str__()方法。然后这个表示将替换“屏幕对象(4)”

class Film(models.Model):
    ageRatings =[
        ("U", "U"),
        ("PG", "PG"),
        ("12", "12"),
        ("15", "15"),
        ("18", "18"),
    ]

    id = models.AutoField(primary_key=True)
    title = models.CharField(max_length=255)
    length = models.DurationField()
    rating = models.CharField(max_length=3, choices=ageRatings, default="U")
    genre = models.CharField(max_length=50)
    poster_url = models.CharField(max_length=255, default='')

def __str__(self):
    return self.title
    
class Screen(models.Model):
    id = models.AutoField(primary_key=True)
    cinema = models.ForeignKey(Cinema, on_delete=models.CASCADE, related_name='screens')
    screen_number = models.PositiveSmallIntegerField()
    seating_capacity = models.PositiveIntegerField()

def __str__(self):
    return self.screens.name  # adjust this to your the logic of Cinema model

相关问题