I have a Django model with a method determines if I need to use it or not. I want to filter the table by the value returned from this method. something like this:
filter(object.method())
Is it possible to do something like this?
Related
From the django docs on annotate():
Annotates each object in the QuerySet with the provided list of query
expressions. An expression may be a simple value, a reference to a
field on the model (or any related models), or...
Is it possible to annotate the results of a method for the model?
I've tried like this:
my_queryset.annotate(ann=my_method(request.user))
and
my_queryset.annotate(my_method(request.user))
But I get an error that my_method is not defined. The method exists and works fine normally: object.my_method(request.user)
I think there is a decorator to have a method treated like a field, but I can't seem to find any info on that (it might have been for django template based method calls, so possibly not related)
An alternate solution is provided in this question. But I would like to know if it is possible to annotate method results.
I have a model that will be displayed in a ListView. One of the fields in the model is category which has 3 choices. When I display the template, I want to be able to tell between the 3 categories. I considered overriding get_context_data() to add a context for each category by getting all the objects and filtering them.
Would that be a better approach than a custom template tag?
And even if it is, assuming I still want to create the custom template tag to accomplish this, how would I write it? I know how to write custom tags, but I am unsure how to write one such as this.
Only idea I can come up with is to create a tag something like:
#register.simple_tag(name="is_cat1", takes_context=True)
def is_cat1(self, context):
objs = context['object_list']
if (MyModel.objects.filter(category__icontains="Cat1") in objs):
objs = MyModel.objects.filter(category__icontains="Cat1")
return objs
Could anyone provide an example of such a filter that deals with models like this? As well as answer my question as to whether it would be better to use context?
Thanks
Using AJAX is the correct solution. Such an operation doesn't fit with Django's framework and intended use.
Trying to modify a Django project's filter.
I have the following query
queryset = Post.objects.filter(public=True)
I want to modify this to have the query ordered by 'like_count'
How would I implement this?
By using order_by, like this:
q = Post.objects.filter(public=True).order_by('like_count')
If Like is a different table.
The solution in the below link might help.
Django, annotation and ordering using data from other table
I have two Model's which are related with a ForeignKey field.
Let's call these objects Event and EventRegistration.
I can easily, for example, do this:
EventRegistration.objects.filter(event=Event.objects.get(name="Some Event"))
But, I cannot do something like this:
EventRegistration.objects.filter(event=Event.objects.all())
I know this is a contrived example, but is there a way to filter against whole QuerySets in a similar way to the second line of code?
EventRegistration.objects.filter(event__in=Event.objects.all())
I have a db table which has an integer array. But how can I add this field in my model? I tried writing it using IntegerField but on save it is giving error
int() argument must be a string or a number, not 'list
How can I add this field to my model? I am using this field in my views.py so I need to add it in my model. Any suggestions?
You may be interested in using a CommaSeparatedIntegerField.
If you've got a list of integers like this:
my_ints = [1,2,3,4,5]
and a model like this:
class MyModel(models.Model):
values = CommaSeparatedIntegerField(max_length = 200)
then you can save my_ints into a MyModel like this:
m = MyModel(values = ','.join(my_ints))
m.save()
I would look into database normalization. In particular, your database is not even in 1st normal form, the first and probably most significant of the normal forms which states that normalized data should not contain any repeating groups. As a result, the Django object-relational-mapper will have considerable difficulty modeling your data.
By supporting only single, non-repeating types, Django in a sense enforces 1st normal form in data. You could try to write your own SQL to manage this particular field or perhaps find some code on the internet, but perhaps better would be to refactor this field into a many-to-one relationship in its own model. You can find Django documentation on this here.
Clueless' answer is probably the best you can get, but in case you still want to store array of numbers in single field, you can do this - either by manually e.g. pickling it and then storing to TextField, or by writing custom model field that do something like this for you automatically.
Here's the doc: http://docs.djangoproject.com/en/1.1/howto/custom-model-fields/
I got it working by saying textfield in my model.Since i am only using that field for reading it doesnot effect me