ãã¢ãã«ä½æãDjango åå¿è ãã³ãã¥ããã£ãµã¤ãçãªä½ãã«ãã£ã¬ã³ã¸ãã¦ã¿ã
ã³ã¬ã¾ã§ã®å±¥æ´
ä¸åç®ããã¼ã¿ãã¼ã¹ã¢ãã«ãä½ã£ã¦ã¿ãã
ãã®åã«ãå
ã使ããããããã¹ãã¨ãã£ã¿æ¢ãã¦ãããã
ç¥ãåãå
ã¨ãã§èãååã¯ãããªæãã ã£ããIDE ãæ··ãã£ã¦ããã©æ°ã«ãã¦ã¯ãããªãã
- PyDev
- PyScripter
- Intype
- KomodoEdit(俺ãæç¨)
ä½æ
ããã§å§ãããã£ã¦ï¼ãã£ãã»ãã楽ã§ããããã
å ã¿ã«ãã®Blogã§ã¹ã¯ãªã¼ã³ã·ã§ããåãã¨ãã¯KomodoEditãªã®ã§ãããããã
å ãã¯ãã¢ããªã±ã¼ã·ã§ã³ã宣è¨ãã
å
ãã¯ã¢ããªã±ã¼ã·ã§ã³ã宣è¨ãã¦ã¿ãã
åå°ã ãã§ã¯ã©ããæ§ããªããããããã
ã¨ãããã¨ã§ãã³ã³ã½ã¼ã«ã§ã¢ããªã±ã¼ã·ã§ã³ã宣è¨ãã¦ã¿ãã
ã³ãã³ãã¯ãããªæã
manage.py startapp community
ã³ãã¥ããã£ã¨ããååã®ã¢ããªã±ã¼ã·ã§ã³ãéå§ããã¨ã
ééãã¦ãã¾ã£ãå ´åã¯ãä½æããããã£ã¬ã¯ããªä»¥ä¸ãåé¤ããã°OKï¼
community ãã£ã¬ã¯ããªã®ä¸èº«ã¯ä»¥ä¸ã®éãã
- __init__.pyãåæåãã¡ã¤ã«(ç¹ã«ä½¿ããªã)
- models.pyãã¢ãã«ãã¡ã¤ã«(ããã« DB å®ç¾©ã¨ãæ¸ãã¦ãã)
- tests.pyããã¹ãã³ã¼ã(ã¦ããããã¹ãç¨ã«ããè¦ããªã)
- views.pyããã¥ã¼ã³ã¼ã(Django ã® View ã¯ã³ã³ããã¼ã©çãªããã)
tests.py ã¯ã¤ã³ã¹ãã¼ã«ã®ã¨ãããdjango-command-extensionsããå ¥ããå½±é¿ããªï¼(ããã©ã«ãã ã¨ç¡ãã£ãæ°ããã)
ã¢ãã«ãä½ã£ã¦ã¿ã
ã§ãã¢ãã«ãä½æãã¦ã¿ãã
Symfony(PHP)ãªããyml ç·¨éãã¦ã³ã³ã½ã¼ã«ã³ãã³ããRubyOnRails(Ruby)ãªãã³ã³ã½ã¼ã«ã§ã³ãã³ãä¸çºã
Django ã®å ´åã¯ãã¢ãã«ãæ¸ãã(RubyOnRails ã§ã¯ãã¢ãã«ã¯ã©ã¹ã¨ãDBé©ç¨ç¨ã®ãã¤ã°ã¬ã¼ãã¯ã©ã¹ã®äºã¤ãããããDjango ã¯ã¢ãã«ã ãæ¸ãã°ããããã¤ã°ã¬ã¼ãã¯ã©ã¹ã¨ãã¦ãé©ç¨ã§ãããããããæå³ã§ã¯åãããããã¦ããã¨æã£ãã)
models.py ãããã«å½ããã
å
ããããã©ã§ãããã®ã¯ä½ãã¨èãã¦ã¿ãã¨ãååã® admin è¦ãã°ããã
- å§
- å
- 権é
- ã¡ã¼ã«ã¢ãã¬ã¹
ããã¯å ã ãããããã¨ãããã¨ã§ãã³ãã¥ããã£ãµã¤ãã§ä»ã«è¶³ããªããã®ãèããã
- ãã¬ã³ãç»é²
- ããã¯ãã¼ã
- ãã¼ã ãã¼ã¸
- 趣å³
- ç¹æ
- é½éåºç
æããã¨ããªããªããããããã¨ãããã°ãå¿
é ã£ã½ããã®æå¤ã¯ãå¾ãã質å追å ã§ããå½¢ãããã®ããªã¼ã
ä¿åããã®ã¯RDBã ããåæã«æ¡å¼µã¨ãããã«ããã³ãèãã¦ãåãé¢ãããã®ã¯åãé¢ãã
ããã¬ã³ãç»é²ãã¯ã¦ã¼ã¶éãç¹ãããã¼ã¿ãªã®ã§ãã¦ã¼ã¶æ
å ±ã«ãã£ã¤ãããªãã¿ãããªèãæ¹ãã¨ãã
ã¾ãç¬æã¨åè¦ã§ãããªãã®ãæå®ãã¨ãã
- ããã¯ãã¼ã
- ãã¼ã ãã¼ã¸
ãã¼ã ãã¼ã¸ã¯ä½ãéãããããï¼ã¨æã人ãããã¨æããã©ã俺ã欲ããï½
ã¨ãããã¨ã§ãããªã¢ãã«ãä½ã£ã¦ã¿ãã
# coding:utf-8 from django.db import models from django.contrib import admin from django.contrib.auth.models import User class UserProfile(models.Model): nick_name = models.TextField(unique=True,max_length=64) home_page = models.URLField(verify_exists=False,null=True,blank=True) admin.site.register(UserProfile) # admin 管çãµã¤ãã«ç»é²
ãã¦ãã©ããã£ã¦ã¦ã¼ã¶ãã¼ãã«ã¨ç´ä»ãããããã¨æ©ãã ããã³ã¬ã
http://djangoproject.jp/doc/ja/1.0/topics/auth.html?highlight=auth#storing-additional-information-about-users
å
¬å¼ããã¥ã¡ã³ãããã£ãããã¦ãã®ã¯ãããã¨ãã
ã¨ãããã¨ã§ä»¥ä¸ã®ããã«å¤æ´ã
# coding:utf-8 from django.db import models from django.contrib import admin from django.contrib.auth.models import User class UserProfile(models.Model): target_user = models.ForeignKey(User,unique=True) nick_name = models.TextField(unique=True,max_length=64) home_page = models.URLField(verify_exists=False,null=True,blank=True) admin.site.register(UserProfile)
ããã¦ãå¤å settings.py ãããã ãããï¼
AUTH_PROFILE_MODULE ãã»ãããããã¤ãã§ã«ãã¢ããªã±ã¼ã·ã§ã³ã®ç»é²ããã¦ãããã
AUTH_PROFILE_MODULE = 'community.userprofile' INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.admin', 'community', )
ä»ã«ãå¿
è¦ãªã®ã§ç¹æ®ãªã®ã¯ãã¬ã³ããªã³ã¯ã ãããï¼
ForeignKey ã®ä½¿ãæ¹ã¯ãã£ãã®ãåç
§ãã¦ãã£ã¦ã¿ãã
# coding:utf-8 from django.db import models from django.contrib import admin from django.contrib.auth.models import User class UserProfile(models.Model): target_user = models.ForeignKey(User,unique=True) nick_name = models.TextField(unique=True,max_length=64) home_page = models.URLField(verify_exists=False,null=True,blank=True) class UserLink(models.Model): target1 = models.ForeignKey(User,unique=False) target2 = models.ForeignKey(User,unique=False) admin.site.register(UserProfile) admin.site.register(UserLink)
ãã®è¾ºã§ä¸å®ãªã®ã§ãmanage.py syncdbããããã¦ã¿ã(ãã®æä½ã§DBã«é©ç¨ãã)ã
ããã¨ãäºã¤ã»ã©ã¨ã©ã¼ãåºãã
Error: One or more models did not validate: community.userlink: Accessor for field 'target1' clashes with related field 'Use r.userlink_set'. Add a related_name argument to the definition for 'target1'. community.userlink: Accessor for field 'target2' clashes with related field 'Use r.userlink_set'. Add a related_name argument to the definition for 'target2'.
é¢ä¿ãã¯ã©ãã·ã¥ãããã related_name ãè¨å®ããªããã¨ã®ãã¨ã
# coding:utf-8 from django.db import models from django.contrib import admin from django.contrib.auth.models import User class UserProfile(models.Model): target_user = models.ForeignKey(User,unique=True) nick_name = models.TextField(unique=True,max_length=64) home_page = models.URLField(verify_exists=False,null=True,blank=True) class UserLink(models.Model): target1 = models.ForeignKey(User,unique=False,related_name='target1') target2 = models.ForeignKey(User,unique=False,related_name='target2') admin.site.register(UserProfile) admin.site.register(UserLink)
ããã§åé¡ãªãéã£ãã
æå¾ã«ãSNS çãªè³ªåã¨ããã®çµæã®è¨é²ãã£ã¼ã«ãã§ãç¨æãã¦ã¿ããã¨æã£ãã
以ä¸ã®æã
- ProfileSubset : 質åã¨ã質åã®åºåãå ¥åã¿ã¤ã
- ProfileSubsetValues : ProfileSubset ãé¸æåã ã£ãå ´åã®é¸æè¢
- UserProfileSubset : ã¦ã¼ã¶ã¨è³ªå(ããã³ãã®è§£ç)ã®ç´ä»ã
çµæã³ã¼ãã¯ãããªæãï¼
# coding:utf-8 from django.db import models from django.contrib import admin from django.contrib.auth.models import User class UserProfile(models.Model): target_user = models.ForeignKey(User,unique=True) nick_name = models.TextField(unique=True,max_length=64) home_page = models.URLField(verify_exists=False,null=True,blank=True) def __unicode__(self): return self.nick_name class UserLink(models.Model): target1 = models.ForeignKey(User,unique=False,related_name='target1') target2 = models.ForeignKey(User,unique=False,related_name='target2') def __unicode__(self): return self.target1.name + ' to ' + self.target2.name # Question and question type class ProfileSubset(models.Model): # Profile view name name = models.TextField(unique=True,null=False,max_length=32) # profile data type(text:0 , radio:1 , chexkbox:2 , intval:3) type = models.IntegerField(null=False) # Question Rank rank = models.IntegerField(null=False) def __unicode__(self): return self.name # UserProfile Type extend values # raido , checkbox class ProfileSubsetValues(models.Model): # Profile subset parent = models.ForeignKey(ProfileSubset) # subset value value = models.TextField(max_length=32) def __unicode__(self): return self.parent.name + " : " + self.value # User inform class UserProfileSubset(models.Model): # taeget user user = models.ForeignKey(User) # target subset subset = models.ForeignKey(ProfileSubset) # subset value value = models.TextField(max_length=255) def __unicode__(self): return str(self.user) + " : " + self.subset.name + "=" + self.value admin.site.register(UserProfile) admin.site.register(UserLink) admin.site.register(ProfileSubset) admin.site.register(ProfileSubsetValues) admin.site.register(UserProfileSubset)