Tuesday 15 February 2011

python - Is there a way to specify abstract functions which take in values for __init__ -


I was not sure how to fully specify this in the title, please forgive me basically, I One method would like to define a get_object () method, as described here

But for an example, in an abstract way, let's say that I am using the APIView such as < / P>

  class as BeterAPIView (APIView): def get_object (self, pk): try: return *** class name ***. Receive (PK = PK): Extend HTTP 404   

So that when I define a view, this is an automated definition for get_object method. For example, if I have a thought for a blog post

  class BlogDetail (BetterAPIView): def find (self, request, pk, format = none): post = Post.get_object (pk )   

I know that the get_object method should be automatically aware that Post.get_object (pk) means that I want to get the object from the post model, as well as the received method With PK in I think that maybe blog dictate should have some type of init method which defines how the name of the model is being used, but I do not even know how it model property you need to set up? Class BeterAPIView (APIView): Model = any DRF __int __ (auto, * AGR, ** keywords): If self.model is none: raise someSortOfConfigurationException super (BetterAPIView, Self) .__ init __ (* args, ** kwargs) def get_object (self, pk): try: return self.model.objects.get (pk = pk) hold: raise http404

And now: Category BlogDetail (BetterAPIView): Model = Post DF (self, request, pk, format = none): post = self.get_object (pk) < Of course, this is a tough example, you want to make it stronger ...

No comments:

Post a Comment