#To_Use
Explore tagged Tumblr posts
Text
Django: check whether an object already exists before adding
How do I check whether an object already exists, and only add it if it does not already exist?
Here's the code - I don't want to add the follow_role twice in the database if it already exists. How do I check first? Use get() maybe - but then will Django complain if get() doesn't return anything?
current_user = request.userfollow_role = UserToUserRole(from_user=current_user, to_user=user, role='follow')follow_role.save()
https://codehunter.cc/a/django/django-check-whether-an-object-already-exists-before-adding
0 notes
Photo
#Beauty_Begins when the #moment you #decide to Use #Lakme. #Beautiful_Skin Requires #Commitment, Not a #Miracle So.... #keep_calm and #Galm_On shoplocalstore.com
Follow Us On:
FaceBook : http://bit.ly/slsFb
GooglePlus : http://bit.ly/sLsGplus
Youtube: http://bit.ly/sLs_uTube Twitter : http://bit.ly/sLs_TwiTTer Tumblr : http://bit.ly/sls_TumBlr Instagram : http://bit.ly/sLs_InSta
0 notes
Photo
Best Pens ever! #Catholic #office #afraid #to_use
0 notes
Text
django form got multiple values for keyword argument
I have a simple model as follows:
RATING_CHOICES = zip(range(1, 6), range(1, 6))class Rating(models.Model): value = models.IntegerField(choices=RATING_CHOICES) additional_note = models.TextField(null=True, blank=True) from_user = models.ForeignKey(User, related_name='from_user') to_user = models.ForeignKey(User, related_name='to_user') shared_object = models.ForeignKey(ObjectDetail, null=True, blank=True) dtobject = models.DateTimeField(auto_now_add=True)
From the above model I generate a model form, in my forms.py as follows:
class RatingForm(ModelForm): class Meta: model = Rating exclude = ('from_user', 'dtobject', 'shared_object')
In my urls I try the following:
url(r'^rate/(?P<form_type>[\w]+)/(?P<oid>\d+)/(?P<oslug>[\w-]+)/$', 'rating_form', name='rating_form'),
And in my views, the following:
def rating_form(form_type = None, oid = None, oslug=None): print form_type form = RatingForm(data=request.POST or None) if request.POST and form.is_valid(): form.save() return HttpResponseRedirect("/") else: return render(request, "share.html", {'form' : form })
Doing this gives me the following error:
rating_form() got multiple values for keyword argument 'form_type'
Additional Details:
Request Method: GETRequest URL: http://127.0.0.1:8000/rate/lending/3/random-stuff/Django Version: 1.4.1Exception Type: TypeErrorException Value: rating_form() got multiple values for keyword argument 'form_type'Exception Location: /Library/Python/2.7/site-packages/django/contrib/auth/decorators.py in _wrapped_view, line 20Python Executable: /usr/bin/python
What am i doing wrong?
https://codehunter.cc/a/django/django-form-got-multiple-values-for-keyword-argument
0 notes