TAGS :Viewed: 8 - Published at: a few seconds ago

[ saving in admin does not work (Mezzanine 3.1.10) ]

Any help appreciated. I cannot save parent Program object in admin.py. It only does not work for 1 particular model with inlines, and only with one particular inline. If I take ProgramHotelInline out, I can save the model. If ProgramHotelInline is in inlines for ProgramAdmin, I can't save the model; the terminal output gives: "POST /admin_keywords_submit/ HTTP/1.1" 403 2294

Here is the PageAdmin class:

class ProgramHotelInline(admin.StackedInline):
    model = ProgramHotel
    extra = 5
    def get_formset(self, request, obj=None, **kwargs):
        initial = []
        formset = super(ProgramHotelInline, self).get_formset(request, obj, **kwargs)
        #initial c/i-c/o
        if obj:
            if request.method == "GET":
                for bh in range(0,self.extra):
                    initial+=[{'check_in':datetime.combine(obj.base_arrival,time(14,00)),
                    'check_out':datetime.combine(obj.base_departure,time(12,00))}]
                self.extra = len(initial)
            formset.__init__ = curry(formset.__init__, initial=initial)
        return formset
    def queryset(self, request):
        """
        Prefetch info.
        """
        qs = super(ProgramHotelInline, self).queryset(request)

        return qs.select_related('hotel__organization','roomtype')
program_extra_fieldsets = ((None, {"fields": ("base_arrival","base_departure","program_type","default_commission","content","no_bed_age","free_child_age","short_description","images")}),)


class ProgramAdmin(PageAdmin):
    inlines = (CodeInline,ProgramExcursionInline,ProgramTransferInline, ProgramHotelInline,ProgramCommissionInline,ProgramDiscountInline)
    fieldsets = deepcopy(PageAdmin.fieldsets) + program_extra_fieldsets
    filter_horizontal = ('images', )
admin.site.register(Program,ProgramAdmin)

here are the models:

class ProgramHotel(models.Model):
    program = models.ForeignKey(Program,null=True,blank=True)
    hotel = models.ForeignKey('tour.Hotel')
    roomtype = ChainedForeignKey(
        'tour.RoomType',
        chained_field="hotel",
        chained_model_field="hotel",
        show_all=False,
        auto_choose=False,blank=True,null=True,help_text="<span style='color:blue'>null means no bed</span>")#null stands for (child) with no bed!
    check_in = models.DateTimeField(default=default_ci_time)
    check_out = models.DateTimeField(default=default_co_time)
    #max_age =  models.IntegerField(default=0)
    pax_per_room = models.SmallIntegerField()
    exb = models.BooleanField(default=False)
    bb = models.BooleanField(default=False)
    turn = models.SmallIntegerField(default = 1)#if we have more than one stopping, first stopping - turn=1,2nd - turn=2 etc
    USD = '$'
    KRW = unichr(8361)
    CURRENCY_CHOICES = (
        (USD, 'USD'),
        (KRW, 'KRW'),
    )
    currency = models.CharField(default = "$",max_length=1,choices=CURRENCY_CHOICES)
    price = models.DecimalField('price per pax, including comm.',decimal_places=0, max_digits=21, default = 0)#in USD
    def __unicode__(self):
        if self.roomtype:
            if self.pax_per_room == 2:
                rt= u"\u00BD%s" % (self.roomtype.roomtype)
            elif self.pax_per_room == 3:
                rt= u"\u2153%s" % (self.roomtype.roomtype)
            elif self.pax_per_room == 4:
                rt= u"\u00BC%s" % (self.roomtype.roomtype)
            elif self.pax_per_room == 5:
                rt= u"\u2155%s" % (self.roomtype.roomtype)
            elif self.pax_per_room == 1:
                rt= u"%s, single occupancy" % (self.roomtype.roomtype)
            elif self.pax_per_room > 5:
                rt= u"1/%s %s" % (self.pax_per_room,self.roomtype.roomtype)
            else:
                rt= u"%s" % (self.roomtype.roomtype)
        else:
            rt="no bed"
        bb = u", breakfast" if self.bb else u", NO BREAKFAST"
        exb = u", +extra bed" if self.exb else u""
        return u"%s: %s, %s ~ %s%s%s; %s%s" % (self.hotel.organization,rt,self.check_in.strftime("%d %B %Y"), self.check_out.strftime("%d %B %Y"),bb,exb,self.price,self.currency)
    class Meta:
        unique_together = (("program", "turn","hotel","roomtype","check_in","pax_per_room","exb","bb"),)
class Program(Page,RichText):
    def default_commission_default():
        l = list(DynamicParameter.within_dates.within_dates(datetime.today()))[:1]
        dynamic = l[0]
        return dynamic.default_agent_commission
    def no_bed_age_default():
        l = list(DynamicParameter.within_dates.within_dates(datetime.today()))[:1]
        dynamic = l[0]
        return dynamic.default_no_bed_age
    def free_child_age_default():
        l = list(DynamicParameter.within_dates.within_dates(datetime.today()))[:1]
        dynamic = l[0]
        return dynamic.default_free_child_age
    program_type = models.ManyToManyField(ProgramType)
    images = models.ManyToManyField(PicasaGallery,null=True, blank=True)
    short_description = RichTextField(max_length=250,default = "")
    #excursions, transfers, hotels - just a template; actuals are in Code
    hotels = models.ManyToManyField('tour.Hotel', through='ProgramHotel')
    transfers = models.ManyToManyField('misc.Transfer', through='ProgramTransfer')
    excursions = models.ManyToManyField("tour.Excursion", through='ProgramExcursion')
    commission = models.ManyToManyField(Customer, through='ProgramCommission')



    default_commission = models.DecimalField('agent commission',decimal_places=3, max_digits=4, default = default_commission_default)
    base_arrival = models.DateField()
    base_departure = models.DateField()
    no_bed_age = models.SmallIntegerField(default=no_bed_age_default) #UP TO THIS AGE ALLOWED TO HAVE NO BED
    free_child_age = models.SmallIntegerField(default=free_child_age_default) #NO BED+BREAKFAST FREE, IF ADULTS HAVE BREAKFAST; no tickets!
    discount = models.ManyToManyField(Discount, through='ProgramDiscount')
    def save(self, *args, **kwargs):
        ''' On save, make invisible in left and foot menu
        SET COMMISSION, IF DEFINED
        add bonus for a company, if any
        '''
        self.in_menus = '1'
        super(Program, self).save(*args, **kwargs)
        if self.default_commission:
            for customer in Customer.objects.filter(customer_type="AGENT"):
                try:
                    pc = ProgramCommission.objects.get(program=self,customer=customer)
                except ObjectDoesNotExist:
                    pc = ProgramCommission(program=self,customer=customer, commission=self.default_commission)
                #print pc
                if not pc.id:
                    if pc.customer.organization=="Private Customer":
                        pc.commission = 0.00
                    try:
                        customer_comm = CustomerCommission.within_dates.within_dates(self.base_arrival).get(customer=customer).bonus_commission
                        if customer_comm >0:
                            pc.commission = pc.commission + customer_comm
                    except ObjectDoesNotExist:
                        pass
                    pc.save()

    def __unicode__(self):
        return u"%s" % (self.title)
    def groupcode(self):
        """
        return filtered set - only code.group=True
        """
        return self.code_set.filter(group=True,arr_date__gte=datetime.today())

    def distincthotel(self):
        """
        return filtered set - only distinct hotels
        """
        return self.programhotel_set.distinct('hotel')

    def get_admin_url(self):
        return urlresolvers.reverse("admin:%s_%s_change" %
            (self._meta.app_label, self._meta.module_name), args=(self.id,))

    class Meta:
        ordering = ['title']

Answer 1


Found the source of mistake: that was outdated django-smart-selects. After update it works again.