1

I have three models in Django, One is default django user model.

class ConfigurationDB(models.Model):
    project_id = models.CharField(max_length=250)

    def __unicode__(self):
        return str(self.project_id)


class ProjectAssociated(models.Model):
    user_name = models.ForeignKey(User, verbose_name="User Name")
    configuration = models.ManyToManyField(ConfigurationDB, verbose_name= "Project Associated")

    def __unicode__(self):
        return str(self.user_name)

I got username from the url, I want to write a Django Query to get the project Associted with that user, and store in a list.

1
  • I have done User.objects.get(username="ashish") and got the list by associated_project_list = user1.associated_projects.all() , But still I got [<AssociatedProject: ashish>] , but what I need is the project_Id which is a character , I have tried loop also , but still doesn't get the desire result. Commented Dec 17, 2015 at 13:15

3 Answers 3

1

Try this;

pa = ProjectAssociated.objects.filter(user_name__username=username)

And to get all configuration;

configuration = pa.configuration.all()

Here you will get the queryset of configuration and if you want only the list of project_id, then you can try this;

configuration = pa.configuration.all().values_list('project_id', flat=True)
Sign up to request clarification or add additional context in comments.

10 Comments

And then how to get configurations for that user
@AshishK. Updated the answer.
hi Jacob, Thanks for your help , but still I am facing issue :( AttributeError: 'ProjectAssociated' object has no attribute 'UserName'
change UserName to user_name
pa = ProjectAssociated.objects.filter(user_name__username=username)
|
1

First, you should fetch a user object, to make sure that the username passed actually exists. Then, query directly by that user object.

from django.shortcuts import get_object_or_404, render

def your_view(request, username=None):
   user = get_object_or_404(User, username=username)
   pa = ProjectAssociated.objects.filter(user_name=user)

   return render(request, 'template.html', {'projects': pa})

Comments

0

Do Simply:

ProjectAssociated.objects.filter(user_name__username=username)

And you don't need any additional work to store it in list, filter by default return a List.

Tipp: in relation such as ForeignKey it's better to use related_name attribute as follow:

user_name = models.ForeignKey(User,verbose_name="User Name", related_name="associated_projects")

this can be useful in case you have the user instance you can access the reverse the relation directly:

user1 = User.objects.get(username=username_value) # here all the user data
associated_project_list = user1.associated_projects.all() # here the list of his projects

to access a project id you need simply:

for project in associated_project_list:
    print project.id

7 Comments

Hi @DhiaTN , from user1 = User.objects.get(username=username) , this will give username. when I tried to do associated_project_list = user1.associated_projects.all() , I am not getting the List. user1 = User.objects.get(username="ashish") associated_project_list = user1.associated_projects.all() >>> print associated_project_list [<AssociatedProject: ashish>] How Can I get the list of project associated.
User.objects.get(username=username) means you need to replace username by a value and you did it correct, which is in you case username="ashish". You already got the list correctly. [<AssociatedProject: ashish>] is the list and contains only one element.
ashish is containing two project , I need to find out that project Id which is associated with ashish
I updated the code, to see how to get the id, You need only to iterate over the list. And based on your result 'ashish' has only one project, so verify your database.
'ashish' has two projects ,i have verified it by db and also by model class ProjectAssociated: def __unicode__(self): return u"%s (%s)" % (self.username, u", ".join([i.projectId or i in self.configurationdb.all()])) but not able to get in view
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.