Posts

Showing posts with the label django

django tagging

django update string value with using existing value

it's possible to use F queries to make updates with using existing values in database, it's quite easy if value is number but when you want to do updates on string values things are getting complicated  a bit Here below I am replacing empty char coming after comma in location field such as London, UK We need to use django's special functions for this purpose F Value and Func from django.db.models import F, Func, Value Developer.objects.filter(location__contains=", ").update(location=Func(         F('location'),         Value(', '), Value(','),         function='replace',     )) Result will be London,UK which is our intended solution

django haystack index update only new items

When solr index becomes bigger, update_index is taking some time and in this period search is returning empty resultset to prevent this basicly you only need to include to the index latest modified records in database. following changes in your model and index structure help you to achieve fast index updates models.py class MyModel ( models . Model ): user = models . ForeignKey ( User ) modified = models . DateTimeField ( auto_now = True ) # add this field to your model search_indexes.py class MyModelIndex ( SearchIndex ): text = CharField ( document = True , use_template = True ) user = CharField ( model_attr = 'user' ) # add this function to your search_indexes.py def get_updated_field ( self ): return "modified" ./manage.py update_index --age=110   indexing will run only items updated in the last 110 hours.

Django mysql innodb transaction

Development is really headache without transactions in database. change your mysql database storage engine to innodb and add commit on success annotation to your procedures in django to work with transaction enabled environment http://djangosaur.tumblr.com/post/357759467/django-transaction-mysql-engine-innodb

How to add pagination to django comments for your model

First step is import Comment and ContentType to your view like below from django.contrib.comments.models import Comment from django.contrib.contenttypes.models import ContentType Second step is defining your model which you want to paginate its comments content_type_id= ContentType.objects.get_for_model( YOUR_MODEL_NAME ) Last get comments of specific instance of your model class with defining object_pk as filter parameter comments = Comment.objects.filter(content_type=content_type_id,object_pk=quote_id) here is complete view code from django.core.paginator import Paginator, InvalidPage, EmptyPage from django.contrib.comments.models import Comment from django.contrib.contenttypes.models import ContentType def quote_show_page(request,quote_id): quote = get_object_or_404(Quote, id=quote_id) content_type_id=ContentType.objects.get_for_model(Quote) comments = Comment.objects.filter(content_type=content_type_id,object_pk=quote_id) paginator = Paginator(comments, 5) # Show 5 comments pe...