在django中保存表单数据后,在保存到新表字段时丢失了我的数据

时间:2017-09-05 17:02:32

标签: python django

当我付款时,数据会保存在django的付款模式中。 然后我想在预订模型和收入模型中保存表格中的数据。

数据保存在payfast模型中,但似乎丢失了我的数据。

以下是所有3个模型,即发送的数据和我的视图逻辑

Payfast模型

class PayFastOrder(models.Model):

# see http://djangosnippets.org/snippets/2180/
__metaclass__ = readable_models.ModelBase

# Transaction Details
m_payment_id = models.AutoField(primary_key=True)
pf_payment_id = models.CharField(max_length=40, unique=True, null=True, blank=True)
payment_status = models.CharField(max_length=20, null=True, blank=True)
item_name = models.CharField(max_length=100)
item_description = models.CharField(max_length=255, null=True, blank=True)
amount_gross = models.DecimalField(max_digits=15, decimal_places=2, null=True, blank=True)
amount_fee = models.DecimalField(max_digits=15, decimal_places=2, null=True, blank=True)
amount_net = models.DecimalField(max_digits=15, decimal_places=2, null=True, blank=True)

# The series of 5 custom string variables (custom_str1, custom_str2...)
# originally passed by the receiver during the payment request.
custom_str1 = models.CharField(max_length=255, null=True, blank=True)
custom_str2 = models.CharField(max_length=255, null=True, blank=True)
custom_str3 = models.CharField(max_length=255, null=True, blank=True)
custom_str4 = models.CharField(max_length=255, null=True, blank=True)
custom_str5 = models.CharField(max_length=255, null=True, blank=True)

# The series of 5 custom integer variables (custom_int1, custom_int2...)
# originally passed by the receiver during the payment request.
custom_int1 = models.IntegerField(null=True, blank=True)
custom_int2 = models.IntegerField(null=True, blank=True)
custom_int3 = models.IntegerField(null=True, blank=True)
custom_int4 = models.IntegerField(null=True, blank=True)
custom_int5 = models.IntegerField(null=True, blank=True)

# Payer Information
name_first = models.CharField(max_length=100, null=True, blank=True)
name_last = models.CharField(max_length=100, null=True, blank=True)
email_address = models.CharField(max_length=100, null=True, blank=True)

# Receiver Information
merchant_id = models.CharField(max_length=15)

# Security Information
signature = models.CharField(max_length=32, null=True, blank=True)

# Utility fields
created_at = models.DateTimeField(default=datetime.now)
updated_at = models.DateTimeField(default=datetime.now)
request_ip = models.GenericIPAddressField(null=True, blank=True)
debug_info = models.CharField(max_length=255, null=True, blank=True)
trusted = models.NullBooleanField(default=None)
user = models.ForeignKey(User, null=True, blank=True)

class HelpText:
    m_payment_id = "Unique transaction ID on the receiver's system."
    pf_payment_id = "Unique transaction ID on PayFast."
    payment_status = "The status of the payment."
    item_name = "The name of the item being charged for."
    item_description = "The description of the item being charged for."
    amount_gross = "The total amount which the payer paid."
    amount_fee = "The total in fees which was deducated from the amount."
    amount_net = "The net amount credited to the receiver's account."

    name_first = "First name of the payer."
    name_last = "Last name of the payer."
    email_address = "Email address of the payer."

    merchant_id = "The Merchant ID as given by the PayFast system."
    signature = "A security signature of the transmitted data"

def save(self, *args, **kwargs):
    self.updated_at = datetime.now()
    return super(PayFastOrder, self).save(*args, **kwargs)

def __unicode__(self):
    return u'PayFastOrder #%s (%s)' % (self.pk, self.created_at)

class Meta:
    verbose_name = 'PayFast order'
    verbose_name_plural = 'PayFast orders'

预订模型

class Bookings(models.Model):

OPTIONS_STATUS = (('y', "Yes"), ('n', "No"), ('p', "Pending"),)

user = models.ForeignKey(User, on_delete=models.CASCADE)
event = models.ForeignKey(Events, on_delete=models.CASCADE)
eventdate = models.DateField()
event_amount = models.CharField(max_length=50, blank=True, default='')
guests = models.IntegerField()
bookingstatus = models.CharField(max_length=50, default='p', blank=True, choices=OPTIONS_STATUS)
created_date = models.DateTimeField(auto_now_add=True)
modified_date = models.DateTimeField(auto_now=True)

盈利模式

 class Earnings(models.Model):

STATUS = (('a', "Active"), ('p', "Pending"), ('d', "Denied"),)
TYPE = (('p', "Payment"), ('r', "Refund"), ('w', "Withdraw"),)

user = models.ForeignKey(User, on_delete=models.CASCADE)
status = models.CharField(max_length=1, default='p', choices=STATUS)
created_date = models.DateTimeField(auto_now_add=True)
modified_date = models.DateTimeField(auto_now=True)
transaction_type = models.CharField(max_length=1, default='', choices=TYPE)
event = models.ForeignKey(Events, on_delete=models.CASCADE, default='')
amount = models.IntegerField()
amount_gross = models.IntegerField()
amount_fees = models.IntegerField()

然后在成功付款后,我的通知网址被调用并被带到此视图

@csrf_exempt
def notify_handler(request):

"""
Notify URL handler.

On successful access 'payfast.signals.notify' signal is sent.
Orders should be processed in signal handler.
"""

id = request.POST.get('m_payment_id', None)
order = get_object_or_404(PayFastOrder, pk=id)

form = NotifyForm(request, request.POST, instance=order)
if not form.is_valid():
    errors = form.plain_errors()[:255]
    order.request_ip = form.ip
    order.debug_info = errors
    order.trusted = True
    order.save()
    raise Http404

order = form.save()

if order.pk is not None:

    obj = Bookings()  # gets new object
    obj.user_id = request.POST.get('custom_str1', None)
    obj.event_id = request.POST.get('custom_str2', None)
    obj.eventdate = request.POST.get('custom_str3', None)
    obj.guests = request.POST.get('custom_str4', None)
    obj.event_amount = request.POST.get('custom_str5', None)
    # finally save the object in db
    bookings = obj.save()

    if bookings.pk is not None:

        objearning = Earnings()  # gets new object
        objearning.user_id = request.POST.get('custom_str1', None)
        objearning.event_id = request.POST.get('custom_str2', None)
        objearning.transaction_type = 'p'
        objearning.amount = int(request.POST.get('custom_str5', None)) - ((14 * int(request.POST.get('custom_str5', None))) / 100)
        objearning.amount_gross = int(request.POST.get('custom_str5', None))
        objearning.amount_fees = ((14 * int(request.POST.get('custom_str5', None))) / 100)
        # finally save the object in db
        objearning.save()

        signals.notify.send(sender=notify_handler, order=order)

        return HttpResponse()

所以这会在我的模型“order = form.save()”中保存,但其余的数据似乎丢失了。

0 个答案:

没有答案