Reputation: 15524
I have the following two models, that should have many-to-many between them:
class Blueprint(models.Model):
name = models.CharField(max_length=120)
description = models.TextField()
class Meta:
ordering = ["name", ]
class Workload(models.Model):
blueprints = models.ManyToManyField('Blueprint', db_constraint=False)
name = models.CharField(max_length=120)
description = models.TextField()
image = models.CharField(max_length=120)
flavor = models.CharField(max_length=120)
class Meta:
ordering = ["name", ]
Being totally new to django and tastypie, I have a couple of questions:
1) What is the proper syntax to specify many-to-many between the Blueprint and Workload, so when a new Blueprint record is created, it does not run into a database constrains, complaining that the related Workload record can not be empty
2) What is the proper syntax for my api.py, to have the related Workload link as a part of GET /api/blueprint/1 output, as well as to have a Blueprint link as a part of GET /api/workload/1 output
I would imagine, that having many-to-many is very common to django / tastypie apps, and it has been solved long ago, but, I was unable to find a clean solution to this (and I'new :)
Upvotes: 0
Views: 102
Reputation: 2122
Not sure what issues you are having, but you should add primary keys to each model, typically an auto int field.
class Blueprint(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=120)
description = models.TextField()
class Meta:
ordering = ["name", ]
class Workload(models.Model):
id = models.AutoField(primary_key=True)
blueprints = models.ManyToManyField('Blueprint', db_constraint=False)
name = models.CharField(max_length=120)
description = models.TextField()
image = models.CharField(max_length=120)
flavor = models.CharField(max_length=120)
class Meta:
ordering = ["name", ]
You should be able to create a blueprint without a workload. However, with current syntax you Can Not have a workload without a blueprint assigned. If you want to allow workloads with no blueprint, then add null=True, blank=True
to that field.
as for the url; there is tons of posts on here that show how to setup a url like your asking.
Upvotes: 1