2017-08-08 6 views
0

正しい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では動作しません。また、関数を使って計算を試みたが、 "期待されるテーブルまたはクエリーセット、関数ではない"が得られた。他に何を試してみるべきですか?

+0

sqlite3/distinctの組み合わせでは何が動作しませんか? – Jieter

+0

「PostgreSQLのみでは、DISTINCTを適用するフィールドの名前を指定するために、位置指定の引数(*フィールド)を渡すことができます」私はsqllite3とmysqlを試しましたが、どちらもうまくいきませんでした。いくつかの回避策を試しましたが、最終的にプロジェクトをPostgreSQLに変換することにしました。このデータベースでは期待通りに動作します。 – jonin

答えて

0

テーブルを作成するときは、必ずQuerySetまたはdictsのリストを渡す必要があります。 django-tables2が関数を取得することに戸惑う場合は、その関数から返される値ではなく、関数に関数を渡しています。

+0

答えをありがとう。私はあなたの声明に同意しますが、sqllite3 dbの "DISTINCT"コールを複製するためにQuerysetを関数で編集できることを期待していました。私は道があると確信していますが、私はついに諦めてdbを変換しました。 – jonin

関連する問題