顯示具有 django 標籤的文章。 顯示所有文章
顯示具有 django 標籤的文章。 顯示所有文章

2014年3月4日 星期二

[django]ViewDoesNotExist: Could not import django.views.generic.create_update.create_object. Parent module django.views.generic.create_update does not exist.


在使用Django 1.5 時發現這個 error message
djen of django 上的例子是使用比較舊版的Django,
在 1.5 class base view 有多個改進。

ViewDoesNotExist: Could not import django.views.generic.create_update.create_object. Parent module django.views.generic.create_update does not exist.


Migrating function-based generic views | Django documentation | Django
https://docs.djangoproject.com/en/1.4/topics/generic-views-migration/

2014年2月13日 星期四

[python][django]model get_or_create return tuple object and created(boolean)


get_or_create

get_or_create(**kwargs)
A convenience method for looking up an object with the given kwargs, creating one if necessary.
Returns a tuple of (object, created), where object is the retrieved or created object and created is a boolean specifying whether a new object was created.

ref:
QuerySet API reference | Django documentation | Django
https://docs.djangoproject.com/en/1.5/ref/models/querysets/#django.db.models.query.QuerySet.get_or_create
python - How does the get_or_create function in Django return two values? - Stack Overflow
http://stackoverflow.com/questions/4706697/how-does-the-get-or-create-function-in-django-return-two-values
django - Correct way to use get_or_create? - Stack Overflow
http://stackoverflow.com/questions/1941212/correct-way-to-use-get-or-create

2014年2月12日 星期三

[python][Django]django.core.urlresolvers utility functions 解析 url


django.core.urlresolvers utility functions 有四個function (Django 1.5)

不外乎因為要是url 改變,可能不只要更改url.py的maping還要修改view的redirect的位置。
所以,這裡使用了

reverse()

If you need to use something similar to the url template tag in your code, Django provides the following function:
reverse(viewname[urlconf=Noneargs=Nonekwargs=Nonecurrent_app=None])
viewname is either the function name (either a function reference, or the string version of the name, if you used that form in urlpatterns) or the URL pattern name. Normally, you won’t need to worry about the urlconf parameter and will only pass in the positional and keyword arguments to use in the URL matching. 
傳入 args list或者傳入 kwargs dict ,根據 view 幫你產生 url 的位置。
舉例來說,
你有個view , post
>>> reverse("post",args=1200)
/post/1200

下面有個更詳細的解釋。
ref:
what is reverse() in Django - Stack Overflow
http://stackoverflow.com/questions/11241668/what-is-reverse-in-django
[转]浅谈reverse函数与django哲学 - 麻烦处... - ITeye技术网站
http://xiaolin0199.iteye.com/blog/585470


2014年2月11日 星期二

[python][django] Managers


class Manager
Manager is the interface through which database query operations are provided to Django models. At least one Manager exists for every model in a Django application.

Managers | Django documentation | Djangohttps://docs.djangoproject.com/en/dev/topics/db/managers/
You can override a Manager‘s base QuerySet by overriding theManager.get_query_set() method. get_query_set() should return a QuerySet with the properties you require.
Django 1.6 後 get_query_set() 改成 get_queryset()

2014年1月24日 星期五

[python][django] _set.all()


在使用Django 你可能換看到 _set.all() 的 描述。

"Django also creates API accessors for the “other” side of the relationship – the link from the related model to the model that defines the relationship. For example, a Blog object b has access to a list of all related Entryobjects via the entry_set attribute: b.entry_set.all().
All examples in this section use the sample Blog, Author and Entrymodels defined at the top of this page."

Making queries | Django documentation | Django
https://docs.djangoproject.com/en/1.5/topics/db/queries/

2014年1月22日 星期三

[python][django] django forms super


在使用 django forms 時,有看到幾個django中super的用法,查了一些相關資料。


It’s important to remember to call the superclass method – that’s thatsuper(Blog, self).save(*args, **kwargs) business – to ensure that the object still gets saved into the database. If you forget to call the superclass method, the default behavior won’t happen and the database won’t get touched.



順帶查了 *args, **kwargs

It’s also important that you pass through the arguments that can be passed to the model method – that’s what the *args, **kwargs bit does. Django will, from time to time, extend the capabilities of built-in model methods, adding new arguments. If you use *args, **kwargs in your method definitions, you are guaranteed that your code will automatically support those arguments when they are added.


Models | Django documentation | Django
https://docs.djangoproject.com/en/1.5/topics/db/models/
Programmer blog: Python super() method and class inheritance
http://garmoncheg.blogspot.tw/2012/06/python-super-method-and-class.html
Understanding Python’s ‘super’ | These are Dark Times, Lad
http://blog.timvalenta.com/2009/02/understanding-pythons-super/