これが私のクラスモデルです:
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(...) を呼び出すと、2 つのオブジェクトがデータベースに作成されます (管理者とデータベースを確認しました)。manage.py shell
Request オブジェクトを 1 つだけ介して呼び出すと、期待どおりに作成されます。私は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 のドキュメントと問題をチェックしましたが、何も見つかりませんでした)