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())
Related
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?
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 form like this on Django app:
class CustomForm(forms.Form):
field1 = forms.ModelChoiceField(queryset=ModelA.objects.filter(type=A))
field2 = forms.ModelChoiceField(queryset=ModelA.objects.filter(type=B))
The Debug Toolbar tells me there are two duplicates querys on ModelA but the filter conditions it's different. Is this a bug?. Also I was wondering if there is a way to optimize this case and make only one query
Thanks!
ModelA.objects.filter(type=A) and ModelA.objects.filter(type=B) are two separate querysets, so require two queries.
In theory, you could do
ModelA.objects.filter(type__in=[A, B])
Which would get all objects where type=A or type=B. You could then filter the list in Python. However, this wouldn't necessarily perform any better. You wouldn't be able to use the ModelChoiceField any more, so your code would be more complicated.
I have a Django form that uses a different number of fields based on the year/month. So I create the fields in the form like this:
for entry in entry_list:
self.fields[entry] = forms.DecimalField([stuffhere])
but now I don't know how to get the submitted data from the form.
Normally I would do something like:
form.cleaned_data["fieldname"]
but I don't know what the names of the fields are. The debug screen shows my POST data as simply "Entry Object" with a value of "u''". Calling POST.lists() doesn't show anything.
I am sure I am missing something obvious, but I've been stuck on this for a few days too many. Is there a better way to do this? Is all of the data in the request object, but I just don't know how to use it?
Here is the code for the model/form/view: http://pastebin.com/f28d92c0e
Much Thanks!
EDIT:
I've tried out both of the suggestions below. Using formsets was definitely easier and nicer.
I think you might be better off using formsets here. They're designed for exactly what you seem to be trying to do - dealing with a variable number of items within a form.
In this line:
self.fields[entry] = forms.DecimalField(max_digits=4, decimal_places=1, label=nice_label)
entry is a model instance. But fields are keyed by field names (strings). Try something like:
self.fields[entry.entry_name] = forms.Decimal(...)
(substitute appropriate for "entry_name").
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