Django inlineform not adding cinema field info on save

20 views
Skip to first unread message

drifter

unread,
Sep 29, 2015, 8:42:34 AM9/29/15
to Django users

I have a inline form which looks like the snapshot above but when I save the "Cinema, Movie type and Price" doesn't get added to the additional fields (each starts time is another instance of the field). For example the second line withĀ 2015-10-02Ā Cinema2Ā will only be saved on the instance of ShowĀ 11:00so it only saves on the first instance.

This is the model Show:

class Show(models.Model):

    MOVIE_TYPES = (
        ('2', '2D'),
        ('3', '3D'),
    )

    CINEMAS = (
        ('C1', 'Cinema 1'),
        ('C2', 'Cinema 2'),
        ('C3', 'Cinema 3'),
        ('C4', 'Cinema 4'),
    )

    movie = models.ForeignKey('Movie', related_name='shows')
    venue = models.ForeignKey(Venue)
    old_details = models.TextField(blank=True, editable=False,)
    starts = models.DateField(db_index=True,)
    starts_time = models.TimeField(db_index=True,)
    couple_price = models.IntegerField(blank=True, null=True)
    vip = models.IntegerField(blank=True, null=True)
    cinema = models.CharField(
        max_length=10,
        choices=CINEMAS,
        blank=True,
        null=True
    )
    comment = models.TextField(blank=True, )
    booking_url = models.URLField(blank=True, )
    created = models.DateTimeField(editable=False, db_index=True,)
    modified = models.DateTimeField(editable=False, db_index=True,)
    price = models.IntegerField(blank=True, null=True)
    new_price = models.IntegerField(blank=True, null=True)
    movie_type = models.CharField(
        max_length=1,
        choices=MOVIE_TYPES,
        blank=True,
        null=True
    )

    class Meta:
        ordering = ['starts', 'starts_time',]
        verbose_name = 'movie show'

    def __unicode__(self):
        return u'%s - %s - %s %s' % (self.movie.name, self.venue.name, self.starts, self.starts_time)

    def save(self):
        if not self.id:
            self.created = datetime.datetime.now()
        self.modified = datetime.datetime.now()
        super(Show, self).save()

    def movie_type_str(self):
        if not self.movie_type:
            return '-'
        else:
            return self.movie_type + 'D'

and the form...

class ShowInlineForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        super(ShowInlineForm, self).__init__(*args, **kwargs)
        theatres = Category.objects.get(slug='movie-theatres')
        self.fields['venue'].queryset = Venue.objects.filter(categories=theatres, visible=True,).order_by('name')

    class Meta:
        fields = [
            'venue',
            'price',
            'movie_type',
            'starts',
            'cinema',
            'starts_time',
        ]

in the admin.py

class MovieShowInline(admin.TabularInline):

    formfield_overrides = {
        models.TimeField: {'widget': AdminTimeWidget(format='%H:%M')},
        models.TextField: {'widget': TextInput()},
    }
    model = Show
    extra = 1
    template = 'admin/movies/show/inline_edit/tabular.html'
    form = ShowInlineForm
    exclude = ('comment', 'booking_url',)

    def queryset(self, request):
        """Do not show shows older than today """

        qs = super(MovieShowInline, self).queryset(request)
        return qs.filter(starts__gte=datetime.date.today()).order_by('venue__name', 'starts', 'starts_time')

class MovieShowAdmin(CommonAdmin):
    fields = ('name',)
    readonly_fields = ('name',)
    list_display = [
        'name',
        'modified_by',
        'created_by',
        'modified',
        'created',
        'visible',
    ]
    inlines = [MovieShowInline,]
    list_filter = ['modified', 'created', 'visible', 'showing']

and the formĀ admin/movies/show/inline_edit/tabular.html

{% load i18n admin_static admin_modify %}
<style>.vTimeField { width:40px !important; }</style>
<div class="inline-group" id="{{ inline_admin_formset.formset.prefix }}-group">
  <div class="tabular inline-related {% if forloop.last %}last-related{% endif %}">
{{ inline_admin_formset.formset.management_form }}
<fieldset class="module">
   <h2>{{ inline_admin_formset.opts.verbose_name_plural|capfirst }}</h2>
   {{ inline_admin_formset.formset.non_form_errors }}
   <table>
     <thead><tr>
     {% for field in inline_admin_formset.fields %}
       {% if not field.widget.is_hidden %}
         <th{% if forloop.first %} colspan="2"{% endif %}{% if field.required %} class="required"{% endif %}>{{ field.label|capfirst }}
         {% if field.help_text %}&nbsp;<img src="{% static "admin/img/icon-unknown.gif" %}" class="help help-tooltip" width="10" height="10" alt="({{ field.help_text|striptags }})" title="{{ field.help_text|striptags }}" />{% endif %}
         </th>
       {% endif %}
     {% endfor %}
     <th colspan="5"></th>
     <th>{% trans "Copy?" %}</th>
     {% comment %}
     {% if inline_admin_formset.formset.can_delete %}<th>{% trans "Delete?" %}</th>{% endif %}
     {% endcomment %}
     </tr></thead>

     <tbody>
     {% for inline_admin_form in inline_admin_formset %}
        {% if inline_admin_form.form.non_field_errors %}
        <tr><td colspan="{{ inline_admin_form|cell_count }}">{{ inline_admin_form.form.non_field_errors }}</td></tr>
        {% endif %}
        <tr class="form-row {% cycle "row1" "row2" %} {% if inline_admin_form.original or inline_admin_form.show_url %}has_original{% endif %}{% if forloop.last %} empty-form{% endif %}"
             id="{{ inline_admin_formset.formset.prefix }}-{% if not forloop.last %}{{ forloop.counter0 }}{% else %}empty{% endif %}">
        <td class="original">
          {% if inline_admin_form.has_auto_field %}{{ inline_admin_form.pk_field.field }}{% endif %}
          {{ inline_admin_form.fk_field.field }}
          {% spaceless %}
          {% for fieldset in inline_admin_form %}
            {% for line in fieldset %}
Reply all
Reply to author
Forward
0 new messages