私は次のような2つのモデルを持っています:
class Visit(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=65535, null=False)
class Session:
id = models.AutoField(primary_key=True)
visit = models.ForeignKey(Visit)
sequence_no = models.IntegerField(null = False)
Session
モデルにカスタム作成メソッドを2つ書いてほしいので、これを書くとき:
visitor = Vistor.objects.get(id=1)
new_session = Session.objects.create_new_session(visit=visitor)
...Session
その訪問者の次の連続するシーケンス番号、つまり 3 を持つ新しいレコードをテーブルに取得します。サンプル データを次に示します。
VISITOR SEQUENCE_NO
------- -----------
1 1
1 2
2 1
2 2
1 3 (This would be the row that would be created)
Session
もう 1 つは、モデルにカスタムの get メソッドを記述して、次のように記述していたことです。
visitor = Vistor.objects.get(id=1)
new_session = Session.objects.get_session(visit=visitor, sequence_no=3)
...そのビジターの以前のレコードを取得し、シーケンス番号が最大です。ここにいくつかのサンプルデータがあります
VISITOR SEQUENCE_NO
------- -----------
1 1
1 2 (This would be the row that would be fetched)
2 1
2 2
1 3
これを達成する方法を教えてください。このコードはモデルまたはマネージャーに配置する必要がありますか?
みんな、ありがとう。