正しいProject Idを持つQuerysetsのみが表示されるところで、django tables2テーブルが正常に機能しています。私はそれが重複user_idを持っているQuerysetsを除外する必要があります。特定の列の一意の値のdjango tables2の結果をフィルタリングする方法
例:
ユーザA、週1、プロジェクト1
ユーザA、週2、プロジェクト1
ユーザB、週1、プロジェクトである1人の
表示ユーザープロジェクト1に割り当てられました(必要な結果は、行が& bのテーブルです)
私が今までに持っているもの:
#models.py
class Allocation(models.Model):
user_id = models.ForeignKey(Resource)
project_id = models.ForeignKey(Project)
week = models.DateField(default=timezone.now)
allocated_hours = models.IntegerField(default=0)
actual_hours = models.IntegerField(default=0)
#tables.py
class Project_Resource_table(tables.Table):
role = tables.Column(accessor='user_id.resource_type')
name = tables.Column(accessor='user_id.resource_name')
allocation = tables.Column(accessor='total_allocation')
class Meta:
model = Allocation
exclude = ('id', 'user_id', 'project_id', 'week', 'allocated_hours', 'actual_hours')
sequence = ('role', 'name', 'allocation')
#view (project_profile.py)
def project_properties(request, offset):
try:
offset = int(offset)
except ValueError:
raise Http404()
project = Project.objects.get(pk=offset)
table = Project_Resource_table(Allocation.objects.filter(project_id=offset).order_by('user_id'))
return render(request, '../templates/copacity/project_profile.html', {
'table': table,
})
私はset()を使用しようとしましたが、無駄です。また、distinct()を試しましたが、sqlite3 dbでは動作しません。また、関数を使って計算を試みたが、 "期待されるテーブルまたはクエリーセット、関数ではない"が得られた。他に何を試してみるべきですか?
sqlite3/distinctの組み合わせでは何が動作しませんか? – Jieter
「PostgreSQLのみでは、DISTINCTを適用するフィールドの名前を指定するために、位置指定の引数(*フィールド)を渡すことができます」私はsqllite3とmysqlを試しましたが、どちらもうまくいきませんでした。いくつかの回避策を試しましたが、最終的にプロジェクトをPostgreSQLに変換することにしました。このデータベースでは期待通りに動作します。 – jonin