gpt4 book ai didi

python - Django 尝试在另一个模型中保存数据后将数据保存到新模型中

转载 作者:太空宇宙 更新时间:2023-11-03 14:49:23 26 4
gpt4 key购买 nike

大家好,我在过去 3 天里一直在努力完成以下工作:

我无法在本地主机上测试它,因为支付网关需要位于实时服务器上。

数据确实会保存到 payfast 模型中,但不会保存到预订模型中

我的代码如下

预订查看

class ViewBookingSummery(LoginRequiredMixin, FormView):
login_url = '/login/'

def get(self, request, slug, pk, guests):
if pk:
events_data = Events.objects.get(pk=pk)
user = User.objects.get(pk=request.user.id)
if events_data:
formset = EventBookingForm()
forms = PayFastForm(initial={
# required params:
'amount': int(events_data.price) * int(guests),
'item_name': events_data.title,
'name_first': user.first_name,
'name_last': user.last_name,
'email_address': user.email,

'custom_str1': user.id,
'custom_str2': events_data.id,
'custom_str3': events_data.date,
'custom_str4': guests,

# optional params:
'return_url' : settings.DOMAIN_NAME + '/bookings/',
'cancel_url' : settings.DOMAIN_NAME + '/booking-summery/' + slug + '/' + pk + '/' + guests + '/'
# ... etc.
}, user=user)

context = {'events_data': events_data, 'guests': guests, 'formset': formset, 'forms': forms}
return render(request, 'bookings/booking-summery.html', context)
else:
return HttpResponseRedirect('/')
else:
return HttpResponseRedirect('/')

我的支付网关模型(支付成功后保存在数据库中)

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 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.CharField(max_length=50, blank=True, default='')
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 Meta:
app_label = 'bookings'
verbose_name = _("Bookings")
verbose_name_plural = _("Bookings")

付款完成后,以下代码将运行

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()

obj = Bookings() # gets new object
obj.user_id = int(request.POST.get('custom_str1', None))
obj.event_id = int(request.POST.get('custom_str2', None))
obj.eventdate = '1992-03-17'
obj.event_amount = '600'
# finally save the object in db
obj.save()

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

return HttpResponse()

由于某种原因,这不起作用

obj = Bookings()  # gets new object
obj.user_id = int(request.POST.get('custom_str1', None))
obj.event_id = int(request.POST.get('custom_str2', None))
obj.eventdate = '1992-03-17'
obj.event_amount = '600'
# finally save the object in db
obj.save()

如何从表单中获取值以在预订模型中显示

最佳答案

正如我可以看到您在 Booking 模型中保存 obj 的最终代码,您需要注意以下事项。

  1. 确保下面的代码行应返回可解析的 int 字符串,而不是 None 或任何随机字符串,因为 userevent 不是 的可为空字段预订模式

    request.POST.get('custom_str1', None)
    request.POST.get('custom_str2', None)
  2. 由于 eventdate 字段是 DateField,因此它不会接受您传递的任何日期字符串。

    Note: Format strings use Python’s datetime module syntax.

    对于你的情况,它看起来像。

    datetime.strptime('1992-03-17','%Y-%m-%d')

欲了解更多信息,您可以访问https://docs.djangoproject.com/en/1.8/ref/settings/#date-input-formats

关于python - Django 尝试在另一个模型中保存数据后将数据保存到新模型中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45990914/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com