这是我的班级模型:
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 文档和问题,但一无所获)