Muhammed Bilal
Muhammed Bilal

Reputation: 399

Creating unique model for each business

Guys i am new in django this is my first project i am creating accounting app. I need this .For example if some one create busines it must have new
budget expense date fields.

class Business(models.Model):
busines = models.CharField(max_len=60)  

class Add(models.Model):
    budget = models.IntegerField(blank=True, null=True)
    expense = models.IntegerField(default=0)
    date = models.DateField()

Upvotes: 0

Views: 31

Answers (1)

mehdi
mehdi

Reputation: 1150

i try to explain a bit more about @Willem comment. if you want contact between Add instance and Business you need have a new field in Add model with ForeignKey to related Business instance.so :

class Business(models.Model):
    business = models.CharField(max_len=60)  

class Add(models.Model):
    budget = models.IntegerField(blank=True, null=True)
    expense = models.IntegerField(default=0)
    date = models.DateField()
    business = models.ForeignKey(Business, null=True, blank=True)

then every time you want you can create an Add instance by this way:

new_business_instance = Business.objects.create(business='home_renovations')

new_add_instance = Add.objects.create(budget=100, expense=200, date=some_date, business=new_business_instance)

or if you have an Add instance from past and now you want add Business:

some_existence_add.business = instance_of_Business
some_existence_add.save()

for more information about ForeignKey you can read ForeignKey from django doc. and information about making queries through ForeignKey you can read Making queries from django doc too.

Upvotes: 3

Related Questions