2011-06-17 20 views
0

我试图用许多类实现Finanace应用程序(wrt java)。 我有这样的情况下是否有可能在python中使用Django类中的对象集合

class User { 

String name; 
Int age; 
Collection<Accounts> accounts; 

接口账户

然后下面的类实现接口

  1. 储蓄账户
  2. 定期账户
  3. 险账户

的帐户将有用户对象

,因为我的java的人,我想知道我可以使用帐户对象的集合在我的用户类。

又怎么会Django的处理人际关系,使数据库表,如果我使用收集

+0

您是否试图将基于Java的应用程序转换为Django项目?我不确定你在做什么,但你应该从这里开始:https://docs.djangoproject.com/en/1.3/intro/tutorial01/ – zeekay 2011-06-17 06:27:49

+0

其实我已经有了基于java的UML类,现在我必须在Python中编码。我可以管理其他东西,但我只想知道是否可以将对象集合存储在python类中 – 2011-06-17 06:29:47

+1

也许你应该从这里开始:http://docs.python.org/tutorial/ – zeekay 2011-06-17 06:34:12

回答

1

你可能想使用django.contrib.auth,它已经提供了User模型,所以你要到模型写入store additional user information,而不是定义新的User模型。 Django模型(通常)表示数据库表,每个属性表示一个数据库字段。您可以定义模型及其关系,并且Django提供了一个很好的数据库访问API。您通常不会“存储帐户对象的集合”,您可以创建另一个模型并使用字段来描述模型之间的关系。

class UserProfile(models.Model): 
    user = models.ForeignKey(User, unique=True) 
    age = models.IntegerField() 

class Account(models.Model): 
    user_profile = models.ForeignKey('UserProfile') 

然后你会使用Django的API与您的模型工作:

profile = User.objects.get(id=1).get_profile() # get user's profile 
profile.account_set.all() # get all accounts associated with user's profile 
acct = Account() # create a new account 
profile.account_set.add(acct) # add a new account to the user's profile 

Django's tutorial是一个良好的开端,如果你想使用Django对于这个项目,你需要的一些概念事情如何完成。首先可能是learn python的好主意。

相关问题