pagination peculiarities...

2011-04-15 Thread Markus T.
Hi everybody,

I have a strange effect using pagination.

I have a model with events that have a date, time, title, description
field and so on. I display the events using pagination. In the model's
Meta class I tell Django to order by date.

I create a queryset like this:

events = Event.objects.filter(date__gte=date.today())

All events are there in the result. But after I apply pagination, one
event is missing, and another event of the same date comes twice. If I
raise the items_per_page parameter so I get only one page, all events
are there as well.

If I tell Django to order [by date, time, title], all events are
there, using pagination or not.

Can anyone explain this?

Thanks a lot!

Markus

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Binary Post Data to ImageField

2009-12-28 Thread Markus T.
After some digging, I finally worked it out. In case anyone is
interested, here is the code for Django:

from django.core.files.uploadedfile import SimpleUploadedFile

def set_user_image(request, img_id):
  """
  set/update a user's image
  """
  if not request.user.is_authenticated():
return HttpResponse(status=403)

  # get the profile for the logged on user
  profile = get_object_or_404(GenericProfile, user = request.user)

  # instantiate an uploaded file to be passed to the model
  upfile = SimpleUploadedFile("%s.jpg" % img_id,
  request._raw_post_data,
  "image/jpeg")

  # update the user profile
  profile.image.save("%s.jpg" % img_id, upfile, True)

  return HttpResponse()

This code lacks important functionality though, e.g. check if the file
is a valid JPEG image, check if img_id is valid, etc.

So basically all you have to do is create a new SimpleUploadFile
object and initialize it with the raw POST data. Of course the client
has to use the correct format; in Flex, I did something like this:

  var req:URLRequest = new URLRequest(baseURL + "setimage/" + _field);
  req.method = "POST";
  req.data = jpData; /* this is the ByteArray containing JPEG data */
  req.contentType = "image/jpeg";

  var loader:URLLoader = new URLLoader();
  loader.addEventListener(Event.COMPLETE, uploadOK);
  loader.addEventListener(IOErrorEvent.IO_ERROR, uploadFault);
  loader.load(req);

To all you Django gurus out there: is this a sensible approach?

Thanks and happy holidays!

Markus

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.




Binary Post Data to ImageField

2009-12-21 Thread Markus T.
Hi,

I'm trying to save binary POST data to an ImageFile field - so far
with no satisfying success.

I can't seem to convince the client (Flex based image editor) to send
binary data as correct "multipart/form-data"; I only have the option
to send as raw binary data or with POST variables.

My view function happily receives the binary data, and it looks ok. As
far as I understand things, if the uploaded data was sent correctly as
multipart-form-data, Django would create an InMemoryUploadFile object
and pass it in request.FILES. I could use this code to save the image
then:

model_instance.image.save("%s.jpg" % img_id, request.FILES
['user_img'], True)

What sensible approach could I use to update/save the image with
binary POST data? I do not want to create a temporary file, though.

I just don't seem to have enough in-depth Django knowledge to find a
satisfying solution...

Thanks!

Markus

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.




Re: Filter by ForeignKey reference??

2009-02-10 Thread Markus T.

That's it - thanks a lot!!!
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Filter by ForeignKey reference??

2009-02-03 Thread Markus T.

Thanks for your reply!

Your approach returns a list of dictionaries (or tuples if values_list
() is used).
Do you know of a straight forward way that returns a list of model
objects, like filter() or all()?
(I mean something smarter than looping the returned list of tuples and
creating a new list of model objects).

Thanks
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Filter by ForeignKey reference??

2009-02-02 Thread Markus T.

Hi,

I have two simple models:

class Country(models.Model):
  name  = models.CharField(_("Name"), max_length=50,
unique=True)

class Profile(models.Model):
  name  = models.CharField(_("Name"), max_length=50,
unique=True)
  country   = models.ForeignKey(Country)


If I want to create a list of all countries that are actually
referenced (i.e. used by at least one Profile entry), how would I code
that in Django?

Maybe something like (of course this is rubbush):

referenced_countries = Country.objects.exclude
(profile__country__isnull=True)


Thanks for your help
Markus
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Overriding Model's save method: error propagation

2009-01-14 Thread Markus T.

if x > y:

should read

except:

of course...
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Overriding Model's save method: error propagation

2009-01-14 Thread Markus T.

David,

for me, these two links did the trick:
http://docs.djangoproject.com/en/dev/ref/contrib/admin/#adding-custom-validation-to-the-admin
http://docs.djangoproject.com/en/dev/ref/forms/validation/#ref-forms-validation

However, validation as described in the Django docs does not happen in
the model's save method; it happens in the admin form. Above links
explain how to tell the admin layer to use your form class which
provides its own validation method. So you could alter the second
database in the validation method and raise a ValidationError if it
fails.
For example:

models.py:
class MyModel(models.Model):
  [...]

class MyModelAdminForm(forms.ModelForm):
  class Meta:
model = MyModel

  def clean(self):
data = self.cleaned_data

try:
  # alter other DB here

if x > y:
  raise forms.ValidationError(_("X must not be greater than y!"))

return data

admin.py:
class MyModelAdmin(admin.ModelAdmin):
  form = MyModelAdminForm


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---