python - Defining a relationship between one User and other model fields in django -


i've been working on testproject , it's such user can have multiple projects , projects can have many contractors. contractors have first register , available work on different projects.the problem is, after defining user , adding multiple projects along many contractors, if try login registering user, same projects appear second user well, without creating new projects..how can separate particular user's data user's?

here models:

class userprofile(models.model):    ## client user      user=models.onetoonefield(user)      def __unicode__(self):         return self.user.username  class contractor(models.model):    name=models.charfield(max_length=128)    url=models.urlfield()    bill_rate=models.integerfield(default=0)    slug=models.slugfield(unique=true)     def save(self, *args, **kwargs):       self.slug=slugify(self.name)       super(contractor, self).save(*args, **kwargs)    def __unicode__(self):       return self.name    class project(models.model):     contractor=models.manytomanyfield(contractor)     client=models.onetoonefield(userprofile, null=true)     title=models.charfield(max_length=128)     slug=models.slugfield(unique=true)     contractor_hours=models.integerfield(default=0)      def save(self, *args, **kwargs):        self.slug=slugify(self.title)        super(project, self).save(*args, **kwargs)      def __unicode__(self):        return self.title 

and these views registering new user, contractor,and dashboard view:

def user_login(request):    context_dict={}    if request.method=="post":       username=request.post.get('username')       password=request.post.get('password')        user=authenticate(username=username, password=password)        if user:         if user.is_active:             login(request, user)             return httpresponseredirect("/keeper/dashboard")         else:             return httpresponse("your account disabled")       else:         print("invalid login details: {0}, {1}".format(username, password))         return httpresponse("invalid login details supplied")    else:        return render(request, 'keeper/index.html', {})    def addcontractor(request):      context_dict={}      registered=false      if request.method=="post":          contractor_form=contractorform(data=request.post)           if contractor_form.is_valid():              contractor=contractor_form.save()              registered=true          else:              print contractor_form.errors      else:          contractor_form=contractorform()       return render(request, 'keeper/addcontractor.html', {'contractor_form':contractor_form, 'registered':registered})    def dashboard(request):       context_dict={}       project_list=project.objects.filter(client__user=request.user).order_by('-title')       contractor_list=contractor.objects.order_by('-name')       context_dict['projects']=project_list       context_dict['contractors']=contractor_list      return render(request, 'keeper/dashboard.html', context_dict)     def addproject(request):      context_dict={}       if request.user.is_authenticated():      current_user=request.user.username      context_dict['user_name']=current_user      #print current_user      contractor_list=contractor.objects.all()      context_dict['contractor_list']=contractor_list       if request.method=="post":         form=projectform(request.post)         project_title=request.post.get('title')         contractor_value=request.post.get('contractor_selected')         contractor=contractor.objects.get(name=contractor_value)         #print project_title, contractor         if form.is_valid():             form.save(commit=true)             get_recent_project=project.objects.get(title=project_title)             #print get_recent_project             get_recent_project=project.objects.get(title=project_title)             ##print get_recent_project             get_recent_project.client=current_user             get_recent_project.save()             get_recent_project.contractor.add(contractor)             #print get_recent_project.contractor.all()             #print get_clientofproject             return success(request)            else:             print form.errors         else:           form=projectform()           context_dict['form']=form     else:       print "none"    return render(request, 'keeper/addproject.html', context_dict) 

you display full project list, ignoring contractor:

  project_list=project.objects.order_by('-title') 

try filter projects owned logged in user:

  project_list = project.objects.filter(       client__user=self.request.user   ).order_by('-title') 

note show every contractor not associated project.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -

jdbc - Not able to establish database connection in eclipse -