Tuesday, September 29, 2015

Django inlineform not adding cinema field info on save

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 %}

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/bfb85ef7-08d9-41aa-8a29-6f9fa6b31f2f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment