Re: running app in backend

2010-12-08 Thread Rendy Anthony
I recommend you to check on celery if you need to run background process. It
has a good django support and can allows you to run periodic tasks.
On Dec 9, 2010 8:44 AM, "commonzenpython"  wrote:
> hey guys, im trying to run a script in the backend of my django
> project, i have used django signals to call my script and run it when
> foo class is saved, and in the foo class theres a variable that can be
> true or false, if its true it calls an infinte loop that runs a
> function every x seconds, but since this is an infinite loop, its
> keeping the django HTTPRequest active, so the page never finishes
> loading, is there a way to bipass this behavior ?
>
> --
> 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.
>

-- 
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: set model field some value before add

2010-05-26 Thread Rendy Anthony
I think the best way is to override the Model's save method:

class Structure(models.Model):
parent
= models.ForeignKey('site.Structure',blank=True,null=True,default=0)
title = models.CharField(max_length=128,verbose_name=u'Заголовок')
url = models.CharField(max_length=48,verbose_name=u'URL')
ordering = models.IntegerField(blank=True,editable=False,default=1)
level = models.SmallIntegerField(blank=True,editable=False,default=1)

def save(self, *args, **kwargs):
if not self.id:
# If the primary key is not defined, this is a new insertion
self.ordering = 42 # set magic values
self.level = 99
super(Structure, self).save(*args, **args)

2010/5/27 ev 

> class Structure(models.Model):
>parent =
> models.ForeignKey('site.Structure',blank=True,null=True,default=0)
>title = models.CharField(max_length=128,verbose_name=u'Заголовок')
>url = models.CharField(max_length=48,verbose_name=u'URL')
>ordering = models.IntegerField(blank=True,editable=False,default=1)
>level =
> models.SmallIntegerField(blank=True,editable=False,default=1)
>
> I'd like to set Structure.ordering and Structure.level with special
> values, based on something.
>
> Is there way to provide Structure.before_add(self) method? I need this
> method to be called every time after saving model data.
>
> --
> 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.
>
>

-- 
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: Redirect from a called function

2010-05-26 Thread Rendy Anthony
You also need to return the result of bar() from foo. Example:

def bar(request):
   print "In bar"
   return HttpResponseRedirect('"http://example.com";)

def foo(request):
    return bar()

Regards,
Rendy Anthony
http://solyaris.wordpress.com

On Thu, May 27, 2010 at 9:50 AM, shacker  wrote:

> I'm trying to figure out why HttpResponseRedirect doesn't work when
> called from within another function. For example, this works, of
> course:
>
> def foo(request):
>  return HttpResponseRedirect('"http://example.com";)
>
> But this does not:
>
> def bar(request):
>  print "In bar"
>  return HttpResponseRedirect('"http://example.com";)
>
> def foo(request):
>  bar()
>
> In this second case, django will print "In bar" but the redirect does
> not occur - it fails silently. Feel like I'm not understanding
> something fundamental here.
>
> Clues? 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-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.
>
>

-- 
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: ModelForm always calls Model's clean method

2010-05-26 Thread Rendy Anthony
My mistake in the model definition. They are supposed to be foreign keys to
the same model.

Thank you for the explanation. I was expecting something like the errors
variable like in a Form. Unfortunately there is no equivalent "error"
variable in the model validation. Here is what I can do in forms:

class MyForm(forms.Form):
def clean(self):
if self.errors:
return # Skip validation if there are existing errors

if self.cleaned_data['departure'] == self.cleaned_data['arrival']:
raise ValidationError("Can't be the same")

I think the documentation should make it clear that the DoesNotExist
exception might be raised in the clean method when trying to access a
foreign key.

Regards,
Rendy Anthony

On Wed, May 19, 2010 at 9:16 PM, Karen Tracey  wrote:

> On Wed, May 19, 2010 at 1:12 AM, ak37  wrote:
>
>> I just tried the new Model Validation feature in Django 1.2 and I
>> found out that ModelForm always calls the Model's clean method even
>> when the form validation has errors. This causes problems when the
>> model has a foreign key, and the value is validated in the clean
>> method. Consider this example:
>>
>> class Flight(models.Model):
>>aircraft = models.ForeignKey(Aircraft)
>>departure = models.ForeignKey(Departure)
>>arrival = models.ForeignKey(Arrival)
>>
>>def clean(self):
>># There can never be flights departing and arriving to the
>> same place
>>if self.departure == self.arrival:
>>raise ValidationError("Departure is the same as Arrival")
>>
>> class FligthForm(forms.ModelForm):
>>class Meta:
>>model = Flight
>>
>> If the form is submitted with empty values, I will get a DoesNotExist
>> exception when trying to access the self.departure/self.arrival
>> attribute in the clean method. Is this by design? If it is then what
>> is the recommended practice to implement the Model's clean method?
>>
>
>
> Calling the model clean() even though validation errors have already been
> noted is consistent with the way form validation is done: the form-wide
> clean() is called even when validation errors have been raised for
> individual fields. It's up to the form-wide clean() to make sure the values
> it wants to work with survived the earlier cleaning attempts; this is noted
> for example here:
> http://docs.djangoproject.com/en/dev/ref/forms/validation/#cleaning-and-validating-fields-that-depend-on-each-other.
> Similarly a model clean() method must be aware that the values it has may
> not be valid per earlier validation that has been run.
>
> In this specific case one way to deal with the issue would be to use a
> try/except block and catch the DoesNotExist. If one is raised, you may
> assume that that error has already been noted by the form cleaning, and just
> ignore it.
>
> (I am a little confused by your model definition though -- you have
> departure and arrival defined as ForeignKeys to different models, so I don't
> see how they would ever compare as equal, unless your actual code is
> checking some attribute on each rather then equality of the foreign key
> values themselves?)
>
> Karen
> --
> http://tracey.org/kmt/
>
> --
> 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.
>

-- 
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.