这是我的班级模特:
class Request(models.Model):
client = models.ForeignKey(Client, related_name='requests')
driver = models.ForeignKey(Driver, related_name='requests', null=True, blank=True)
fare = models.OneToOneField(Fare, null=True, blank=True)
REQUEST_STATE_CHOICES = ((0, 'NEW'), (1, 'PROCESSING'), (2, 'SUCCEED'), (3, 'FAILED'))
state = models.IntegerField(choices=REQUEST_STATE_CHOICES, default=0)
express = models.BooleanField(default=True)
created = models.DateTimeField(default=datetime.datetime.today())
modified = models.DateTimeField(default=datetime.datetime.today())
request_date = models.DateTimeField(default=None, null=True, blank=True)
origin_lat = models.FloatField(null=True, blank=True)
origin_lon = models.FloatField(null=True, blank=True)
origin_address = models.CharField(max_length=200, null=True, blank=True)
destination_address = models.CharField(max_length=200, null=True, blank=True)
NB_DRIVERS_AROUND = 10
@staticmethod
def createRequest(client, originLat, originLon, originAddress, destinationAddress, requestDate=None, driver=None):
"""
Create request
:param client: the client
:param originLat: origin latitude
:param originLon: origin longitude
:param originAddress: origin address
:param destinationAddress: destination address
:param driver: driver, default=None
"""
request = Request()
request.client = client
request.origin_lat = originLat
request.origin_lon = originLon
request.origin_address = originAddress
request.destination_address = destinationAddress
request.driver = driver
if driver is not None:
request.express = False
if requestDate is None:
request.request_date = datetime.datetime.now()
else:
request.request_date = requestDate
request.save()
当我从视图中调用Request.createRequest(...)时,会在数据库中创建两个对象(我检查了我的管理员和我的数据库)。当我通过manage.py shell
调用它时,只按预期创建一个Request对象。我正在使用Django 1.6和postgresql
以下是views
致电:
def test(request):
client = Client.objects.get(username='brams')
Request.createRequest(client, 36.822718, 10.15789, 'Rue 6449, el omrane', 'le kram', requestDate=None,driver=None)
return render(request, 'test.html', {})
以下是manage.py shell
致电:
>>> from etaxi_prod.models import Request, Client
>>> Request.objects.all()
[]
>>> client = Client.objects.get(username='brams')
>>> request = Request.createRequest(client, 36.822718, 10.15789, 'Rue 6449, el omrane', 'le kram', requestDate=None, driver=None)
>>> request
<Request: Request object>
>>> Request.objects.all()
[<Request: Request object>]
>>>
你知道为什么吗?(我已经花了几个小时查看Django文档和问题但没有发现任何内容)
答案 0 :(得分:0)
是否有可能存在一些“创建”相关的事件,这些事件也会调用你的createRequest?
您可以使用django调试工具栏查看所有事件,它也有助于检查您的变量(例如,您可以在视图中设置日志消息并检查它是否被调用两次)。