django
Receive obj must be an instance or subtype of type when trying to call a class method
I am trying to call a class method in a view. The method generates a url however when I try call the method using get_context_data I receive obj must be an instance or subtype of type when trying to call a class method models.py class Scenario(models.Model): name = models.CharField(max_length=256, blank=False, null=False, unique=True) description = models.TextField(max_length=1000) def get_absolute_url(self): return reverse('scenarios:detail', kwargs={'pk': self.pk}) def __unicode__(self): return self.name def generate_trade_url(self): if self.trade_set.exists(): return reverse('scenarios:trade-scenario', kwargs={'pk': self.pk}) return None views.py class Index(generic.ListView): template_name = 'scenarios/index.html' context_object_name = 'all_scenarios' def get_queryset(self): return Scenario.objects.all().order_by('name', 'description') class Detail(generic.DetailView): model = Scenario context_object_name = "scenario" template_name = 'scenarios/details.html' def get_context_data(self, **kwargs): context = super(Scenario, self).get_context_data(**kwargs) context['tradeurl'] = Scenario.generate_trade_url() return context urls.py url(r'^$', views.Index.as_view(), name='index'), url(r'^(?P<pk>[0-9]+)/$', views.Detail.as_view(), name='detail'), template <tbody> {% for scenario in all_scenarios %} <tr> <td><a href ="{% url 'scenarios:detail' scenario.id %}" data-toogle="tooltip" title="Click for filtered view">{{scenario.name}}</a></td> <td>{{scenario.description}}</td> <td> <a href="{% url 'scenarios:scenario-update' scenario.id %}"> <button type="submit" class="btn btn-default btn-sm"> <span class="glyphicon glyphicon-pencil" /> </button> </a> </td> <td> <form action="{% url 'scenarios:scenario-delete' scenario.id %}" method="post"> {% csrf_token %} <input type="hidden" name="scenario_id" value="{{ scenario.id}}"/> <button type="submit" class="btn btn-default btn-sm" onclick="return confirm('Are you sure you want to delete {{scenario.name}}?')"> <span class="glyphicon glyphicon-trash" /> </button> </form> </td> </tr> {% endfor %} </tbody>
The problem is that you're not calling the method on the specific instance of this model, but the class instead. Simply put: if you don't care about the DB values that this instance of this class contains, you can call either a static or a class method (both having their pros/cons, but you can find info about them elsewhere). If you do care about the DB values of this instance (your case, it seems), then you have to call a bound method of the class. So how? Well, just FYI, static and class methods can be created and called like this: class Something(object): CONSTANT_A = '123' #classmethod def my_class_method(cls): return cls.CONSTANT_A * 123 #staticmethod def my_static_method(): return 'static value' # or duplicate the previous method: return Something.CONSTANT_A * 123 a = Something() class_method_value = a.my_class_method() static_method_value = Something.my_static_method() For the use case that you have, you need a bound method and that's exactly what you have there, but the way you wall it is a bit off. You need an instance of the class (with DB values), not the class itself. In the view you can achieve it like this: def get_context_data(self, **kwargs): value_that_you_want = self.object.generate_trade_url() If you don't have the self.object variable at some point, then one way to get the model instance, is to query it from the database. I.e. Detail.objects.get(id=1). Good luck.
Related Links
All urls going to / when trying to run django/nginx on EC2
jquery ajax / django - present form in a bootstrap modal and re-show if validation was not successfull
Hadoop and Django, is it possible?
How make apache serve static files to django
Setting a datefield a few days back
I can not use django-gearman-commands make the worker work all the time
Django registration : Error register a new user
How can I make a model read-only?
Tastypie - PUT deletes all unsent (unset) fields in object - UPDATE
Django multi-criteria weighted rating
Modify Tables in database
Should Django reverse exceptions yield a 404 or 500?
Properly storing values passed in URL in Django
weird django translations behaviour
ViewDoesNotExist while rendering
sending SMS Text message using django send_mail