You may get the above error message in

—-

from biz.models import Entry, City, Category

class CityAdmin(admin.ModelAdmin):
list_display = (‘name’)
admin.site.register(City, CityAdmin)

———

to fix,

list_display = (‘name’,)

In urls.py

urlpatterns = patterns(’mysite.bizcard.views’,
(r’^about$’, views.simple, {‘template_name’:'about.html’}),
(r’^help/$’, views.simple, {‘template_name’:'help.html’}),

)

In views.py

def simple(request, template_name):

return render_to_response(template_name, {‘book_list’:Book.objects.all()})

——–

Note here that single view function can render multiple template files.

1.

(r’^articles/(\d{4})/(\d{2})/$’, month_archive)

vs.

2.

(r’^articles/(?P<year>\d{4})/(?P<month>\d{2})/$’, month_archive)

The first one: positional argument

The second one: keyword argument

For example, a request to

/articles/2009/10/

would result in

1. month_archive (request, ’2009′,’10′)

2. month_archive(request, year=’2009′, month=’10′)

If multiple view prefixes exist,

add multiple pattern() objects.

——————-

urlpatterns = patterns(’mysite.bizcard.views’,
(r’^$’, ‘home_page’),
(r’^page/$’, ‘listing’),
(r’^bizcardpost/$’, ‘picture_upload’),

)

urlpatterns +=patterns(‘mysite.bizentry.views’,

(r’^entry/$’,'bizentry’),

)

When string technique is used (rather than function) in URLconfs,

to reduce duplication, you can use view prefix.

—–

urlpatterns = patterns(”,
(r’^$’, ‘mysite.bizcard.views.home_page’),
(r’^page/$’, ‘mysite.bizcard.views.listing’),
(r’^bizcardpost/$’, ‘mysite.bizcard.views.picture_upload’),

)

will be changed with view prefix as follows:

urlpatterns = patterns(‘mysite.bizcard.views’,
(r’^$’, ‘home_page’),
(r’^page/$’, ‘listing’),
(r’^bizcardpost/$’, ‘picture_upload’),

)

Two types:

1. function object (need import view function)

2. string (don’t need to import)

——————

from django.conf.urls.defaults import *

from mysite.views import hi

urlpatterns = patterns(‘ ‘,

(r’^hi/$’, hi),

(r’^hello/$’, ‘mysite.views.hello’),

)

——–

Note in the first example,

hi

is a function object. This means you need to import the view function.

In second example,

‘mysite.views.hello’

is a string.

Here you don’t need to import it (Django will figure out)

class Bookmark(models.Model):

user = modedls.ForeignKey(User, related_name=’u_bookmarks’)

————————————-

without related_name,

>>>u= User.objects.get(pk=1)

>>>bookmarks=u.bookmark_set.all()

with related_name

>>>u= User.objects.get(pk=1)

>>>bookmarks=u.u_bookmarks.all()

———

VERY IMPORTANT: here,

‘u_bookmarks’ is a manager which is actually doing the same role for ‘obejcts’

That is,

it can even create a bookmark. For example,

>>>u.u_bookmarks.create(name=’hi’)

Consider

Book – Author

relationship.

————-

class Book(models.Model):

authors=models.ManyToManyField(Author)

—————

>>> b = Book.objects.get(id=50)

>>>b.authors.all()

this provides the list of authors

>>>b.authors.filter(name=’Lee’)

>>>a=Author.objects.get(name=’Lee’)

>>>a.book_set.all()

Similar to Foreign key value.

Use

delete() method

—–

e.g.,

>>>Card.objects.get(name=’Hot Pot’).delete()

>>>Card.objects.filter(city=’USA’).delete()

>>>Card.objects.all().delete()

Note that

>>>Card.objects.delete()

is not working.

Use

update()

on QuerySet.

———–

e.g.,

>>>Card.objects.all().update(country=’USA’)

The update() method has a return value which is an integer that represents how many records changed.

© 2011 Web Oom Suffusion theme by Sayontan Sinha
Switch to our mobile site
Page 2 of 11«12345»10...Last »Top Footer