class TaskManager(models.Manager):
def get_query_set(self):
return super(TaskManager, self).get_query_set().filter(Owner='jim')
class Task(models.Model):
Name = models.CharField('Title', max_length=200)
Notes = models.TextField('Description',max_length=2000, null=True)
project = models.ForeignKey(Project,null=True, blank=True)
Owner = models.CharField(max_length=100, choices=owner_set)
objects = TaskManager()
def __unicode__(self):
return self.Name
I have two models in my models.py, every Task has a project. But when i enter the Task page, it will list all the task by default. So i want it to list only the current user's Task by default. It is that the Task.Owner=current user.
Who can help me with this, thanks!
I have searched from the net and then get the solution:
I update the code i pasted just now above.
Thanks for all.
Why not do this!
Task.objects.filter(owner = current_user)
Update:
1) If you want to filter in you own custom template add this to your view code
2) If you are trying to customize the admin site do this,
from django.contrib import admin
from models import MyModel
class TaskAdmin(admin.ModelAdmin):
def queryset(self, request):
return super(TaskAdmin, self).queryset(request).filter(owner = request.user)
admin.site.register(Task, TaskAdmin)
In the ModelAdmin class you should add:
class TaskAdmin(...):
def queryset(self, request):
qs = super(TaskAdmin, self).queryset(request)
return qs.filter(Owner=request.user)
This will override the default queryset
more here:
https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.queryset
I just want to modify the default filter of a model, make the model list not all items without the modelAdmin, because the modelAdmin just provide a filter which need manual click, i want to filter the model by default automaticlly
Related
I am building a simple social media app. AND i am trying to build a feature of adding users into post using ManyToManyField.
I am trying to access profile friends in Post's model instance "add_user" for tagging user.
models.py
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE,default='',unique=True)
full_name = models.CharField(max_length=100,default='')
friends = models.ManyToManyField("Profile",blank=True)
class Post(models.Model):
post_owner = models.ForeignKey(User,default='',null=True,on_delete = models.CASCADE)
post_title = models.CharField(max_length=500,default='')
add_user = models.ManyToManyField(User.profile.friends.all())
I am new in django and I have no idea how can i access user's friend in Post's model instance.
Any help would be much Appreciated.
Thank You in Advance.
You can't give a queryset as an argument for a ManyToManyField, just a class name.
add_users = models.ManyToManyField(User.profile.friends.all()) # you can't do this.
add_users = models.ManyToManyField(User) # Do this.
You shouldn't place you logic in your model's definition.
Do that in your views.
EDIT:
I suggest you use a ModelChoiceField and do the filtering logic there:
class AddFriendForm(forms.Form):
def __init__(self, *args, **kwargs):
try:
user = kwargs.pop('user')
except:
user = None
super(AddFriendForm, self).__init__(*args, **kwargs)
self.fields['friend'].queryset = user.profile.friends.all()
friend = forms.ModelChoiceField(queryset=User.objects.none())
And then in your view you initialize it like this:
def your_view(request):
form = AddFriendForm(user=request.user)
You cannot do
add_users = models.ManyToManyField(User.profile.friends.all())`
Because the models is evaluated once, and need to be non mutable values
You need to do :
add_user = models.ManyToManyField("Profile",blank=True)
And dont forget to add on your ManyToManyField:
related_name="XXX", null=True
I'm new to django and i'm trying to make a school web app fully inside the django administration.
The thing i'm trying to do is to make a permission for the students that gives them the ability to only see the scores which its student field is same to the student user.
This is my Student model:
class Student(models.Model):
student_name = models.CharField(max_length=12)
student_last_name = models.CharField(max_length=12,null=True)
student_age = models.PositiveSmallIntegerField(null=True)
student_class = models.ForeignKey(Clas,on_delete=models.SET_NULL,null=True)
student_idnum = models.CharField(max_length=15,null=True)
user = models.OneToOneField(User,on_delete=models.SET_NULL,null=True)
pub_date = models.DateTimeField("date published")
def __str__(self):
return self.student_name
And this is my students Score model:
class Score(models.Model):
exam = models.ForeignKey(Exam,on_delete=models.SET_NULL,null=True)
student = models.ForeignKey(Student, on_delete=models.CASCADE,null=True)
score = models.FloatField(null=True)
def __str__(self):
return str(self.score)
I have seen the Meta model which i should use it for the custom permission, but i really don't know what should i do.
The official docs has an example that does what you want.
The get_queryset method on a ModelAdmin returns a QuerySet of all model instances that can be edited by the admin site. One use case for overriding this method is to show objects owned by the logged-in user
from django.contrib import admin
class MyModelAdmin(admin.ModelAdmin):
def get_queryset(self, request):
qs = super().get_queryset(request)
if request.user.is_superuser:
return qs
return qs.filter(author=request.user)
admin.site.register(MyModel, MyModelAdmin)
You can change the queryset to fit your needs.
I am new to Django, Please forgive any silly mistakes in code or logic,
Intro: I am trying to create a user follower model in Django. Where users can follow and unfollow other users on the sites
Error: I have made the models for my follow/unfollow I have also made the views I am getting this error
AttributeError at /accounts/admin/follow/
Cannot use add() on a ManyToManyField which specifies an intermediary model. Use accounts.Contact's Manager instead.
The obj.followers.add(user) is highlighted in the traceback as the origin of the error
Below are my models.py
from django.contrib.auth.models import User
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
city = models.CharField(max_length=100)
country = models.CharField(max_length=100)
def get_absolute_url(self):
return reverse('accounts:profile', kwargs={'username': self.user.username})
class Contact(models.Model):
user_from = models.ForeignKey(User, related_name='suppporter')
user_to = models.ForeignKey(User, related_name='leader')
def __str__(self):
return '{} follows {}'.format(self.user_from, self.user_to)
User.add_to_class('following',
models.ManyToManyField('self', through=Contact, related_name='followers', symmetrical=False))
I think the models.py may be good. The fault I believe is in my views.
Below is my view.py
class FollowToggle(LoginRequiredMixin, RedirectView):
def get_redirect_url(self, *args, **kwargs):
username = self.kwargs.get('username')
print(username + " This is the user who will be followed") # This prints correct
profile = get_object_or_404(Profile, user__username=username)
print(profile) # This prints correct
obj = get_object_or_404(User, username=username)
print(obj) # This prints correct
url_ = profile.get_absolute_url()
print(url_) # This prints correct
user = self.request.user
print(user) # This prints correct
if user.is_authenticated():
if user in obj.followers.all(): # I know this is the source of the error.
obj.followers.remove(user)
else:
obj.followers.add(user)
return url_
Below are the Urls.py just in case
url(r'^(?P<username>[-\w]+)/follow/$', views.FollowToggle.as_view(), name='follow'),
You cannot use add and remove method for manytomany relation defined through third model. From the docs:
Unlike normal many-to-many fields, you can’t use add(), create(), or set() to create relationships
Instead you should use Contact manager:
if user.is_authenticated():
if user in obj.followers.all(): # I know this is the source of the error.
Contact.objects.filter(user_to=obj, user_from=user).delete()
else:
Contact.objects.create(user_to=obj, user_from=user)
In Django 2.2 you can use add, remove and set methods (Docs)
You can also use add(), create(), or set() to create relationships, as long as your specify through_defaults for any required fields
My models.py looks like this:
class Person(models.Model):
Name = models.CharField(max_length=100)
class Lecture(models.Model):
Speaker = model.ForeignKey(Person)
Topic = models.CharField(max_length=100)
Choices = ((1,"Upcoming"),(2,"In Progress",),(3,"Completed"))
Status = models.SmallIntegerField(choices=Choices, default=1, max_length=1)
My admin.py looks like this:
class LectureAdmin(admin.ModelAdmin):
def get_queryset(self):
return Lecture.objects.exclude(Status='Completed')
So my change list view in the django admin for the Lecture model shows only Lectures in "Upcoming" and "In Progress" status. This works fine.
Now I need to get the URL for the list of all lectures to be passed as a view somewhere else.The standard way of doing this in the django admin is by reversing the URL, so I do this:
urlresolvers.reverse('admin:%s_%s_changelist' % (app_label, model_name))
However, when I do this,I get the the filtered Queryset with Lectures in "Completed" state missing.How do I construct a url reverse function to get entire Lecture queryset and not the filtered queryset?
Here's a workaround, looks ugly, I understand.
Add all GET parameter to the changelist url:
url = urlresolvers.reverse('admin:%s_%s_changelist' % (app_label, model_name))
url += '?all'
Call get_queryset() on super(), exclude Completed status only if there is no all in request.GET:
class LectureAdmin(admin.ModelAdmin):
def get_queryset(self, request):
qs = super(LectureAdmin, self).get_queryset(request)
if 'all' not in request.GET:
qs = qs.exclude(Status='Completed')
return qs
UPD (applying other filters from request.GET):
from xadmin.plugin.related import RELATE_PREFIX # or just set RELATE_PREFIX = '_rel_'
qs = qs.filter(**{key[len(RELATE_PREFIX):]: value
for key, value in request.GET.iteritems()
if key.startswith(RELATE_PREFIX)})
** unpacks the dictionary into keyword arguments.
Hope it works for you.
get_queryset() is the basic queryset used in admin listing, thus you wo'nt be able to get all the records if you override it this way.
Possible solutions:
use filters ( https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.filter_vertical ) to exclude unwanted records (these with Status='Completed'
or
create proxy model for Lecture, register it in admin and use modified get_queryset() in given listing. Proxy model is required because each model can have registered only single AdminModel class
models.py
class IncompletedLecture(Lecture):
class Meta:
proxy = True
admin.py
class IncompletedAdmin(admin.ModelAdmin):
def get_queryset():
return Lecture.query.exclude(Status='Completed')
admin.site.register(IncompletedLecture, IncompletedAdmin)
I am relatively new to Django and I'm trying to achieve something that is not quite clear in the documentation. My application requires multiple types of users. So, I have extended django User, creating a Profile model that contains additional fields common in all User types:
USER_TYPES = (
('A', 'UserTypeA'),
('B', 'UserTypeB'),
('C', 'UserTypeC'),
)
class Profile(models.Model):
user = models.OneToOneField(User, unique=True)
about = models.TextField(blank=True)
user_type = models.CharField(max_length=3, choices=USER_TYPES, default='A')
def save(self, *args, **kwargs):
try:
existing = Profile.objects.get(user=self.user)
self.id = existing.id #force update instead of insert
except Profile.DoesNotExist:
print "Profile not created yet"
models.Model.save(self, *args, **kwargs)
def create_user(sender, instance, created, **kwargs):
print "User Profile Creation: False"
if created:
print "User Profile Creation: ", created
Profile.objects.create(user=instance)
post_save.connect(create_user, sender=Profile)
In settings.py I have set:
AUTH_PROFILE_MODULE = 'users.Profile'
After that i have defined my UserTypeX models deriving from Profile models like this:
class UserTypeA(Profile):
phone_number = models.CharField(max_length=13,blank=False)
class UserTypeB(Profile):
company_name = models.CharField(max_length=30,blank=False)
phone_number = models.CharField(max_length=13,blank=False)
def __init__(self, *args, **kwargs):
kwargs['user_type'] = 'B'
super(UserTypeB, self).__init__(*args, **kwargs)
...
I've registered those user models to admin so that I could manage my users independently.
The default admin behavior displays correctly all the Profile and UserTypeX fields and there is a select box (with a plus button next to it) for the User field - due to the OneToOneField relationship between Profile and User models. So whenever I want to create a new UserTypeX, I have to press the plus button and fill in a new popup window all the default User django defined fields.
What I am struggling to do now is display those User fields, not in a new popup window but inline in my UserTypeX add/edit page. I've read the documentation about StackedInlines and TabularInlines but those doesn't fit my case because I want to inline parent fields in an ancestor's add/edit page and not vice versa.
Is there any suggested solution with example code (please!) for that problem? Thank's in advance!
So, to make things short, is there a way to display User fields (instead of the select/add functionality due to OneToOneField relationship) in Profile add/edit screen in admin?
Update: A related question (unanswered though...) that briefly addresses the problem is:
Reverse Inlines in Django Admin
No, you cannot. But you can make B inline in A if you want. Or maybe you can manipulate how django display it in
def unicode(self):
models.py
class A(models.Model):
name = models.CharField(max_length=50)
def __unicode__(self):
return self.name
class B(models.Model):
name = models.CharField(max_length=50)
a = models.ForeignKey(A)
admin.py
class B_Inline(admin.TabularInline):
model = B
class A_Admin(admin.ModelAdmin):
inlines = [
B_Inline,
]
admin.site.register(A, A_Admin)
admin.site.register(B)
Or maybe you want to use many-to-many relationship?
models.py
class C(models.Model):
name = models.CharField(max_length=50)
def __unicode__(self):
return self.name
class D(models.Model):
name = models.CharField(max_length=50)
cs = models.ManyToManyField(C)
admin.py
class C_Inline(admin.TabularInline):
model = D.cs.through
class D_Admin(admin.ModelAdmin):
exclude = ("cs",)
inlines = [
C_Inline,
]
admin.site.register(C)
admin.site.register(D, D_Admin)
I don't see how this is going to work - you have told django to use users.Profile as your profile model but you actually want to use it's children. Plus as you say you're wanting to use the inline forms the "wrong way around". Without knowing more about what exactly you're trying to achieve I'd suggest that you could fix the inline form issue by defining a custom view to edit the user details rather than using django admin. As for the different profile types - I'd suggest just defining a profile type field in the model and then hiding/showing different fields/properties depending on its value