0

その質問はカバーされていると確信していますが、問題を解決するためにスタックに関連するものをすべて試しましたが、成功しませんでした。

これが私のmodel.pyです

from django.db import models
from django.contrib.auth.models import User


class Description(models.Model):
    titre_routine = models.CharField(max_length=100)
    type_routine = models.CharField(max_length=100)
    lieu_routine = models.CharField(max_length=100)
    periode = models.CharField(max_length=100, blank=True)
    routine_description = models.TextField()
    raison_chgmt = models.TextField(blank=True)
    user = models.ForeignKey(User)
    #prendre une liste de routine et faire une liste de choix
    #si non ajout a la liste de routine dans nouvelle table

    def __unicode__(self):
        return self.titre_routine

#User.profile = property(lambda u: Description.objects.get_or_create(user=u)[0])


class Rewards(models.Model):
    jour = models.CharField(max_length=4)
    activite_altr = models.TextField()
    first_word = models.CharField(max_length=100, blank=True)
    second_word = models.CharField(max_length=100, blank=True)
    third_word = models.CharField(max_length=100, blank=True)
    urge_validator = models.BooleanField() #urge satisfait ou non?
    urge_validate_date = models.DateField(auto_now=True)
    description = models.ForeignKey(Description)

    def __unicode__(self):
        return self.jour

私のモデルを説明するには: 1 人の「ユーザー」が複数の「説明」を作成できます。「説明」には複数の「報酬」が含まれています。...「説明」には複数の「その他のもの」が含まれます

ここに私のview.pyがあります

#the user access to his dashboard, dashboard filtered by the user logged in.
@login_required(login_url='/userauth/login/')
def dashboard(request):
    routine_info = Description.objects.filter(user=request.user)
    return render_to_response('dashboard.html', {
        'routine_info': routine_info
    })

#dashboard for the reward section that should be like the first dashboard filtered only by the user logged in AND should only show the reward related to One description created previously by the user.
@login_required(login_url='/userauth/login/')
def reward_dashboard(request):
    reward_info = Rewards.objects.all()
    return render_to_response('rewards_dashboard.html', {
        'reward_info': reward_info
    })


#the user can have access to the reward he created as the other def, he should be the only one to have access (again reward created by the user and related to ONE description)
@login_required(login_url='/userauth/login/')
def reward_description(request, reward_id):
    reward_info = Rewards.objects.get(id=reward_id)
    return render_to_response('reward_description.html', {
        'reward': reward_info
    })

#rewardform, I miss the pieces to obtain de description id that will link both table Rewards and Description (and recursively the User table via Description???)
@login_required(login_url='/userauth/login/')
def new_reward(request):

    if request.POST:
        form = RewardsForm(request.POST)

        if form.is_valid():
            obj = form.save(commit= False)
            obj.description = #Missing piece

            #test that could never happen
            if obj.description == 200:
                return HttpResponseRedirect('/reward_dashboard/')
            else:
                obj.save()

            return HttpResponseRedirect('/reward_dashboard/')

    else:
        form = RewardsForm()

    args = {}
    args.update(csrf(request))

    args['form'] = form

    return render_to_response('create_reward.html', args)

要約すると:

-ユーザーは、自分が作成した報酬にアクセスできます。他の定義として、アクセスできるのは彼だけである必要があります (ここでも、ユーザーによって作成され、1 つの説明に関連する報酬)

-報酬セクションのダッシュボードには、最初のダッシュボードがログインしているユーザーによってのみフィルタリングされ、以前に作成された報酬関連の One の説明のみが表示される必要があります。

-報酬フォーム、テーブル報酬と説明の両方をリンクする説明IDを取得するためのピースがありません(および説明を介してユーザーテーブルを再帰的にリンクします)

セッションを調べてみるべきですか?(ユーザーが説明を作成するとき、私は彼に特定の ID を割り当てますか?)

私の説明で私の主張が明確になったことを願っています。

お手伝いありがとうございます!

4

2 に答える 2

1

これを解決するには、複数の選択肢があります。それは、アプリに実装するワークフローによって大きく異なります。

報酬を作成する前に説明を選択してください

すべてのユーザーの説明を一覧表示するページが表示されます。これがダッシュボード ビューです。ここから、ユーザーは報酬を追加したい説明を選択する必要があります: それをクリックするか、特定の報酬作成リンクをクリックするなど。これはあなた次第です (エルゴ/デザインの選択)。主なポイントは、説明IDを取得し、次のようなnew_rewardURLconf() を介してビューに渡すことです。urls.py

# urls.py
urlpatterns = patterns('',
  # your urlpatterns ...
  url(r'^description/(?P<description_id>\d+)/reward/add/$', 'your.app.views.new_reward', name='add-reward'),
  # your urlpatterns ...
)

これはほんの一例です。URLは自由に設定できます。とにかく、ユーザーがリンクまたはボタン (または選択したもの) をクリックすると、URL はmondomaine.fr/yourappprefix/description/4/reward/addのようなものになります(4 は選択した説明の ID です。そのようなリンクを生成するのはあなた次第です)

これで、ビューで説明オブジェクトを取得できます。

#rewardform, I miss the pieces to obtain de description id that will link both table Rewards and Description (and recursively the User table via Description???)
@login_required(login_url='/userauth/login/')
def new_reward(request, description_id):
    description = Description.objects.get_or_404(id=description_id)
    pass

直接リンクで報酬を作成する

そのようなワークフローがあなたの場合のオプションでさえない場合は、単に説明フィールドを に追加してくださいRewardsForm。これにより、ユーザーは報酬を追加したい説明を狙うことができます。

説明リストを現在のユーザーによって作成されたものだけに制限するには、 views.py でこれを行うことができます:

# your code ...
else:
    form = RewardsForm()
    form.fields['description'].queryset = Description.objects.filter(user=request.user)

form.is_validが返された場合は、保存を行う必要があるかもしれませんFalse

少なくともお役に立てば幸いです。

于 2013-08-17T13:43:58.003 に答える