On 3/10/06, Gavin <[EMAIL PROTECTED]> wrote:

My question: How can I create a foreign key based on the values entered
for other fields?
 
I can't help but feel that I'm missing some subtlety in your requirements, but here's a rough example (exact field details, namespaces etc ommitted for clarity):

*** MODELS.PY

class Account(Model):
   owner = CharField()

class Reimburse(Model):
    price = FloatField()
    count = IntegerField()
    account = ForeignKey(Account)

class Expenditure(Model):
    cost = FloatField()
    account = ForeignKey(Account)
 
*** CREATION CODE

a1 = Account(name='Russ')
a1.save()

r1 = Reimburse(price='1.99', count=3, account=a1)
r1.save()

e1 = Expenditure(cost=( r1.price*r1.count), account=a1)
e1.save()

or, if you want to factor out the common bits:

def createExpenditure(r)
    e = Expenditure(cost=(r.price*r.cost), account=r.account)
    e.save()
    return e

e1 = createExpenditure(r1)

Is that what you were looking for?

Hope this helps,
Russ Magee %-)

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to