I'm using Django 1.5.
I have a table MultiUser
class MultiUser(models.Model):
user = models.ForeignKey(User, related_name='owner_user')
shared_user = models.ForeignKey(User, related_name='shared_user')
access_level = models.CharField(
default='Viewer',
max_length='100',
blank=True
)
This table maps users with access level under owner user. user
field defines the owner user and shared_user
is the mapped user who can be either manager or viewer
I have defined a model manager method to get list of all users including the owner user.
def getSharedUsers(self, user):
# Get owner
owner = None
if self.filter(user=user).exists():
owner = user
elif self.filter(shared_user=user).exists():
owner = self.filter(shared_user=user)[0]
# Get all users
shared_users = []
if owner:
shared_users = self.filter(user=owner)
shared_users.append({
'shared_user': owner,
'access_level': 'OWNER',
'created': owner.created
})
return shared_users
Since owner user has no access_level
defined in the MultiUser model. I want to manually append the owner user to the queryset list with shared_user
field set to the owner user and access_level
as owner.
But this gives error as
'QuerySet' object has no attribute 'append'
It is possible to append manually generated object to the queryset object? If yes how?
Here when you are using shared_users = self.filter(user=owner)
, the shared_users
become a queryset. Well, if you want to append owner to the queryset, you can do it like this:
from itertools import chain
shared_users = list(chain(shared_users, [{
'shared_user': owner,
'access_level': 'OWNER',
'created': owner.created
}])
But down side is, you will lose support functionality of a queryset. So you need to use it like a list.