在管理员中保存不起作用(Mezzanine 3.1.10)

时间:2014-11-28 12:45:36

标签: django mezzanine

任何帮助表示赞赏。 我无法在admin.py中保存父程序对象。它仅适用于具有内联的1个特定模型,并且仅适用于一个特定的内联。如果我将 ProgramHotelInline 取出,我可以保存模型。如果ProgramHotelInline是ProgramAdmin的内联,我无法保存模型;终端输出给出:" POST / admin_keywords_submit / HTTP / 1.1" 403 2294

这是PageAdmin类:

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)

以下是模型:

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']

1 个答案:

答案 0 :(得分:0)

找到错误的根源:那是过时的django-smart-choices。更新后再次运行。