I can inherit from a class based generic view from generic.ListView as in
class BookListView(generic.ListView):
model = Book
Supposedly the .as_view() function gets called when the Django app starts up to create an instance of this view.
I find the definition of get_queryset in MultipleObjectMixin but what I dont find is, from where is this method called from?
Related
I have the following setup right now:
class MyView(MyPermission1, MyPermission2, FormView):
...
class MyChildView(MyView):
...
The child view is inheriting from the parent, but I want to remove MyPermission1 and MyPermission2 from it and apply another class called MyPermissionChild. Is there a way to do this using the generic views available in Django?
I am trying to create a very simple CRUD application using REST API.
So I create a very simple model, serializer and viewset for all these.
And then I noticed that I don't fully understand some basic principals about right use-cases for calling (for example create method for my model instance)
As I understand, django providers several approaches:
I can define my CRUD methods inside model class:
class Foo(models.Model):
...
def create(...):
foo = Foo()
foo.save()
I also can create instances using model serializers (seems there is no big difference, because the same save method from model instance is calling):
class FooSerializer(seializer.ModelSerilizer):
...
class Meta:
model = Foo
....
def create():
fs = self.Meta.model()
fs.save()
2b. I can use simple serializers:
class FooSerializer(serializers.Serializer):
def create(**validated_data):
return Foo(**validated_data)
Finally I can use perform_create, update and so on from viewset:
class FooView(ModelViewSet):
serializer = FooSerializer
def perform_create():
serializer.save()
...
Is there some patterns when one or another solution should be implemented?
Could you please provide some explanation with use cases?
THanks!
Lets go step by step on your points of creating/using create method:
You don't need to write a create() method inside model.
You don't need to write a create() method in model serializer, unless you want to handle additional keywords or override the create() method to change the default behavior(Reference).
In serializer.Serializer you can write a create() method if you want save an instance with that serializer. Useful when you are using this serializer with GenericAPIViews or Viewsets. Reference can be found in documentation.
By writing perform_create() method in Viewset, you are basically overriding the default perform_create() from the Viewset. You can integrate additional tasks inside that function when overriding it(example).
How do I get the current user related object using django's generic class DetailView? Using function based views, I can obtain the object like this:
def foo(request):
friendProfile = get_object_or_404(request.user.profile.friends,username="admin")
What is the equivalent using detail view? I'm guessing it's something related to get_object or get_context_data but I can't fully understand the documents.
Thank you.
request is an attribute of class based views. To get the current user you should use self.request.user.
On a DetailView overload the get_queryset to edit the queryset used to get the object.
I don't know precisely how your friend model is defined but let's assume it has a foreign key pointing to your profile with a related_name set to friend_of. Your view could be:
class FriendProfileDetail(DetailView):
model = Friend
def get_queryset(self):
return self.model.objects.filter(friend_of=self.request.user.profile)
I've coded some classes in pure python, but now I need to use that classes in a django view.
my_custom_classes.py
class RetryException(Exception):
...
...
class Trade():
def __init__(self):
...
...
def some_other(self, id):
...
...
For example I need to make a call to a django model inside the "some_other(self, id)".
What is the best way of organizing this custom classes for using in a Django view?
Best Regards,
There is no difference between using a python class in a django view and using a class inside a "normal" python function.
Instantiate your class, and call its methods.
Do you have a Trade model? If so, would it makes sense to put that functionality in the Trade model class?
If you need to call something inside of your Trade class, what you are calling has to be in scope. If you are querying a model you can import it in the module Trade is defined in, and you can access it as you expect.
from yourproject.yourapp.models import AModel
class Trade(object):
def some_other(self, id):
return AModel.objects.filter(pk=id)
I'm looking to add a method that my Ticket model has called process to the admin, so that I could click a link in the list view, and "process" my model instance (do an API call behind the scenes).
To clarify:
class Ticket(models.Model):
title = models.CharField(max_length=255)
def process(self):
... hardcore processing action ...
I need to add the process() method directly to the admin without using a separate function.
You just need to provide a small method in your ModelAdmin class that returns a link pointing at a view that calls your model method, and add the name of that method to the modeladmin's list_display tuple. You'll obviously also need to define this view itself, and a url that points to it.
Yes, thats possible; Check out this documentation, just what you need:
http://docs.djangoproject.com/en/1.1/ref/contrib/admin/actions/#ref-contrib-admin-actions