logo
Tags down

shadow

How to pass a request.user to a model Manager?


By : Derya
Date : September 16 2020, 02:00 AM
will help you Short answer: The request object is only available in a view, so you can't assume it's available in your model manager.
Pass it from your view to a method in your manager that accepts it:
code :
class BookManager(models.Manager):
    def by_author(self, user):
        return self.get_queryset().filter(author=user)


Share : facebook icon twitter icon

Manager Returns Model Objects Once Per Request (Manager Drops Objects After Returning Them Once)


By : Mathiu
Date : March 29 2020, 07:55 AM
seems to work fine Sorry all, the problem was actually unrelated to the problem as I presented it. I had a careless bug in one of my signals on Entry:
In myapp.__init__:
code :
post_init.connect(entry_initialized, sender=Entry)
def entry_initialized(sender, instance, *args, **kwargs):
    try:
        #Disconnect signal to avoid infinite recursion
        post_init.disconnect(entry_initialized, sender=Entry)

        #Intializing a new Entry here would cause the recursion
        #Check to see if there is a previous entry by this user in this blog
        #In this app entries are unique by (User, Blog)
        #And what we want to do is remove the old Entry if the positive fields don't match
        prev_instance = Entry.objects.get(user=instance.user, blog=instance.blog)

        #This is an error: it is not appropriate to delete without some checking
        prev_instance.delete()

        post_init.connect(verification_initialized, sender=Verification)
    except:
        post_init.connect(verification_initialized, sender=Verification)
        #Only delete if this is a different Entry with same user/blog and the positive field is different.
        if not instance.id == prev_instance and not instance.positive == prev_instance.positive:
            prev_instance.delete()

Django: Model inheriting from base model with custom manager. Can the manager have dynamic variables?


By : user1723640
Date : March 29 2020, 07:55 AM
To fix this issue No. The manager is instantiated as a class attribute, thereby giving all model instances the same manager.

customizing request.user with a proxy model that extends Django User model


By : Kevin Warner
Date : March 29 2020, 07:55 AM
it fixes the issue You can inherit a new middleware class from AuthMiddleware or create a separate middleware which will process request after django's auth and change request.user to your user instance.

django 1.10 custom user model and user manager


By : Partha Nag
Date : March 29 2020, 07:55 AM
will be helpful for those in need Im trying to create a custom user model and his manager , The solition was to specify the missing property on the model
code :
last_login = None
is_active = True

How to pass a custom model manager queryset to a template


By : user3174294
Date : March 29 2020, 07:55 AM
this one helps. I am trying to pass a custom query to a template but the query results are not being displayed. , There is a typo:
code :
context = {'follow_data', follow_data}
context = {'follow_data': follow_data}
def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    context["follow_data"] = context.pop("object_list")
    return context
shadow
Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk