私がモデルを持っているとしましょう:
class Person(models.Model):
id = models.AutoField(primary_key=True)
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
の最初の文字を検索first_name
して、のリストを取得しますPerson
。しかし、それぞれが異なる必要がありますlast_name
。
例:次M...
のリストでの検索:
Frank Edmond
Marc Thomas
Matthew Ronald
Matthew Smith
Matthew Thomas
Richard Thomas
あなたにあげます
Marc Thomas
Matthew Ronald
Matthew Smith
要するに、MySQLで翻訳する方法:
Person.objects.filter(first_name__startswith='M').distinct('last_name')
MySQLでは
SELECT DISTINCT <table>.last_name FROM <table> WHERE <table>.first_name LIKE 'M%'
<table>
テーブル名に置き換えます。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加