I have a Many-to-many relationship with additional fields and I want to be able to change data in these fields (e.g. the status of friendship). How can I do that? All the info I found is about how to just read these data.
class Profile(models.Model):
# other fields
friends = models.ManyToManyField("self", blank=True, through='Friendship',
through_fields=('user', 'friend'),
symmetrical=False,
related_name='user_friends')
class Friendship(models.Model):
user = models.ForeignKey(Profile, on_delete=models.CASCADE, related_name='friendships1')
friend = models.ForeignKey(Profile, on_delete=models.CASCADE, related_name='friendships2')
status = models.PositiveSmallIntegerField(default=0)
class Meta:
unique_together = [['user', 'friend']]
I tried this and that didn't work, though no error was shown:
user = User.objects.get(username=request.user)
watched_user = User.objects.get(id=watched_user_id)
Friendship.objects.filter(user=user.profile, friend=watched_user.profile).status = 5
user.save()
I can't call Friendship.save() as it has no self.
And also I tried this and again no effect and no error:
user.profile.friends.set([watched_user.profile], through_defaults={'status': 5})
user.save()
And this gives me an error that there's no friend field and shows me the fields of Profile, not Friendship:
user.profile.user_friends.get(user=user.profile, friend=watched_user.profile).status=5
Please help me!
Upd: the answer helped and it works now! Thanks!
Though I still wonder if this is the only way or it can also be done from the user.profile side.
You can work with:
friendship = Friendship.objects.get(
user=user.profile,
friend=watched_user.profile
)
friendship.status = 5
friendship.save()
Here however there can be multiple Friendships between the same two users.
We can update all these friendships in bulk with:
Friendship.objects.filter(
user=user.profile,
friend=watched_user.profile
).update(status=5)
Related
I really don't understand all the ways to build the right query.
I have the following models in the code i'm working on. I can't change models.
models/FollowUp:
class FollowUp(BaseModel):
name = models.CharField(max_length=256)
questions = models.ManyToManyField(Question, blank=True, )
models/Survey:
class Survey(BaseModel):
name = models.CharField(max_length=256)
followup = models.ManyToManyField(
FollowUp, blank=True, help_text='questionnaires')
user = models.ManyToManyField(User, blank=True, through='SurveyStatus')
models/SurveyStatus:
class SurveyStatus(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
survey = models.ForeignKey(Survey, on_delete=models.CASCADE)
survey_status = models.CharField(max_length=10,
blank=True,
null=True,
choices=STATUS_SURVEY_CHOICES,
)
models/UserSurvey:
class UserSurvey(BaseModel):
user = models.ForeignKey(User, null=True, blank=True,
on_delete=models.DO_NOTHING)
followups = models.ManyToManyField(FollowUp, blank=True)
surveys = models.ManyToManyField(Survey, blank=True)
questions = models.ManyToManyField(Question, blank=True)
#classmethod
def create(cls, user_id):
user = User.objects.filter(pk=user_id).first()
cu_quest = cls(user=user)
cu_quest.save()
cu_quest._get_all_active_surveys
cu_quest._get_all_followups()
cu_quest._get_all_questions()
return cu_quest
def _get_all_questions(self):
[[self.questions.add(ques) for ques in qstnr.questions.all()]
for qstnr in self.followups.all()]
return
def _get_all_followups(self):
queryset = FollowUp.objects.filter(survey__user=self.user).filter(survey__user__surveystatus_survey_status='active')
# queryset = self._get_all_active_surveys()
[self.followups.add(quest) for quest in queryset]
return
#property
def _get_all_active_surveys(self):
queryset = Survey.objects.filter(user=self.user,
surveystatus__survey_status='active')
[self.surveys.add(quest) for quest in queryset]
return
Now my questions:
my view sends to the create of the UserSurvey model in order to create a questionary.
I need to get all the questions of the followup of the surveys with a survey_status = 'active' for the user (the one who clicks on a button)...
I tried several things:
I wrote the _get_all_active_surveys() function and there I get all the surveys that are with a survey_status = 'active' and then the _get_all_followups() function needs to call it to use the result to build its own one. I have an issue telling me that
a list is not a callable object.
I tried to write directly the right query in _get_all_followups() with
queryset = FollowUp.objects.filter(survey__user=self.user).filter(survey__user__surveystatus_survey_status='active')
but I don't succeed to manage all the M2M relationships. I wrote the query above but issue also
Related Field got invalid lookup: surveystatus_survey_status
i read that a related_name can help to build reverse query but i don't understand why?
it's the first time i see return empty and what it needs to return above. Why this notation?
If you have clear explanations (more than the doc) I will very appreciate.
thanks
Quite a few things to answer here, I've put them into a list:
Your _get_all_active_surveys has the #property decorator but neither of the other two methods do? It isn't actually a property so I would remove it.
You are using a list comprehension to add your queryset objects to the m2m field, this is unnecessary as you don't actually want a list object and can be rewritten as e.g. self.surveys.add(*queryset)
You can comma-separate filter expressions as .filter(expression1, expression2) rather than .filter(expression1).filter(expression2).
You are missing an underscore in surveystatus_survey_status it should be surveystatus__survey_status.
Related name is just another way of reverse-accessing relationships, it doesn't actually change how the relationship exists - by default Django will do something like ModelA.modelb_set.all() - you can do reverse_name="my_model_bs" and then ModelA.my_model_bs.all()
I have a Client object that has a User and a Company objects as FKs
class Client(models.Model):
user = models.OneToOneField(User)
company = models.ForeignKey(Company, blank=True, null=True)
In my CompanyAdmin, I want a list of all clients as a TabularInline, including the active status of each client (based on user object)
class CompanyClients(admin.TabularInline):
model = Client
fields = ('user', 'master')
class CompanyAdmin(admin.ModelAdmin):
inlines = [CompanyClients]
I want to add a column in my TabularInline to indicate that that user is active or not. I tried using 'user__is_active' to my fields but I get this error:
Unknown field(s) (user__is_active) specified for Client
I just want an indication even if read-only (which I tried by putting a readonly_fields and it didn't work too), not looking to save/edit each client user's active status value from the Tabular form.
How can I approach this? Thanks in advance.
class CompanyClients(admin.TabularInline):
model = Client
fields = ('user', 'master','user__active)
user__active
I'm working on a little project using these models here and I'm trying to figure out a way to get a set of all the posts associated with users the currently authenticated user is following.
But I keep getting:
Cannot use QuerySet for "Profile": Use a QuerySet for "User".
class Profile(models.Model):
user = models.OneToOneField(User)
isInstructor = models.BooleanField(default=False)
isTutor = models.BooleanField(default=False)
isStudent = models.BooleanField(default=False)
isAdmin = models.BooleanField(default=False)
following = models.ManyToManyField('self', related_name = "followers", blank=True, symmetrical=False)
profile_image = ImageField(upload_to=get_image_path, blank=True, null=True)
class Post(models.Model):
title = models.CharField(max_length=100)
topic = models.CharField(max_length=50)
description = models.CharField(max_length=1200)
poster = models.ForeignKey(User, related_name="posts")
likes = models.IntegerField(default=0)
created = models.DateTimeField(auto_now_add=True)
tags = models.ManyToManyField(Tag, blank=True, related_name="posts")
def __str__(self):
return self.title
This is what keeps giving me the error.
current_user = Profile.objects.get(user = self.request.user)
Post.objects.filter(poster__in = current_user.following.all())
I searched around an found out that I had to use the __in operator whenever you want to filter by a list of things. But I keep getting the same error. Any help with explaining what the error means and what I can do to get around it would be much appreciated.
Maybe try something like this,
Post.objects.filter(poster__id__in=current_user.following.all().values_list('user_id'))
profile class is different to the user class. Therefore, the Profile instance is different to User's instance.
Instead of use current_user you need to use current_user.user.
You can check the documentation.
This is old, but I do not see a clear explanation of the error yet.
Consider this:
Post.poster is a foreign key to the User model.
current_user is a Profile object, not, as the name would suggest, a User object.
Profile.following is a m2m relation back to Profile, so it represents a Profile queryset.
Thus, when you filter on poster__in=current_user.following.all(), you're actually trying to compare a User with a Profile queryset.
This cannot be done, and Django is telling you exactly that:
Cannot use QuerySet for "Profile": Use a QuerySet for "User".
To fix this, you should provide a User queryset in the filter, e.g. something similar to zaidfazil's answer:
current_user_profile = Profile.objects.get(user=self.request.user)
Post.objects.filter(
poster__in=current_user_profile.following.values('user_id')
)
Or do something like this: https://stackoverflow.com/a/67247647
This does not answer the original post, but may help people who end up here based on the title:
A similar error message can also arise when your lookup refers to a reverse relation using '<fieldname>_set'.
For example, if a Bar model has a foreign key to a Foo model, then Foo will get a default related manager called Foo.bar_set. However, a lookup attempt like foo__bar_set__in=... would yield the following error:
ValueError: Cannot use QuerySet for "Bar": Use a QuerySet for "Foo".
This can be fixed by removing the _set from the lookup, so foo__bar_set__in=... should actually be foo__bar__in=....
I have little experience in django so I would really appreciate your help!
In general, I have created a ModelForm which depending on the users who is logged in, he changes some values in the field. So consider that this form is being edited about 5 times by 5 different users.
I would like to show a specific field in the template (and view) only when the third user is logged in.
My model is :
class Task(models.Model):
Taskdetails = models.CharField(max_length=500, null=True)
asset = models.ForeignKey('Asset', null=True)
failure = models.ForeignKey('Failure', null=True)
cause = models.ForeignKey('Cause', null=True)
Created_task_date = models.DateTimeField(default=timezone.now, null=True)
employee = models.ForeignKey("auth.User", null = True)
and also i have created this ModelForm
class Meta:
model = Task
fields = ('Taskdetails', 'asset', 'failure', ,'employee','cause',)
Also I have 5 edititions of the TaskForm in which is user edits something.
The thing I am trying to do is to show the cause field only in the third form.
I tried to exclude the value but nothing apperas.
If i include the field cause (just like above), I must "pass" it in the template in order to be edited from the first user (otherwise the task_form is not saved)
I hope I became clear.
I would really appreciate your help.
I am using a ModelForm to create a form, and I have gotten the initial values set for every field in the form except for the one that is a ManyToMany field.
I understand that I need to give it a list, but I can't get it to work. My code in my view right now is:
userProfile = request.user.get_profile()
employer = userProfile.employer
bar_memberships = userProfile.barmembership.all()
profileForm = ProfileForm(
initial = {'employer': employer, 'barmembership' : bar_memberships})
But that doesn't work. Am I missing something here?
Per request in the comments, here's the relevant parts of my model:
# a class where bar memberships are held and handled.
class BarMembership(models.Model):
barMembershipUUID = models.AutoField("a unique ID for each bar membership",
primary_key=True)
barMembership = USStateField("the two letter state abbreviation of a bar membership")
def __unicode__(self):
return self.get_barMembership_display()
class Meta:
verbose_name = "bar membership"
db_table = "BarMembership"
ordering = ["barMembership"]
And the user profile that's being extended:
# a class to extend the User class with the fields we need.
class UserProfile(models.Model):
userProfileUUID = models.AutoField("a unique ID for each user profile",
primary_key=True)
user = models.ForeignKey(User,
verbose_name="the user this model extends",
unique=True)
employer = models.CharField("the user's employer",
max_length=100,
blank=True)
barmembership = models.ManyToManyField(BarMembership,
verbose_name="the bar memberships held by the user",
blank=True,
null=True)
Hope this helps.
OK, I finally figured this out. Good lord, sometimes the solutions are way too easy.
I need to be doing:
profileForm = ProfileForm(instance = userProfile)
I made that change, and now everything works.
Although the answer by mlissner might work in some cases, I do not think it is what you want. The keyword "instance" is meant for updating an existing record.
Referring to your attempt to use the keyword "initial", just change the line to:
bar_memberships = userProfile.barmembership.all().values_list('pk', flat=True)
I have not tested this with your code, but I use something similar in my code and it works.