Mazki516
Mazki516

Reputation: 1027

writing a model for existing mssql table Django

I've this tables in a mssql server:

Table name: Items
Fields:
ItemID - primarykey - integer
StorageLocation - varchar(100)

Table name: Inventory
Fields:
InvetoryID - primarykey - integer
ItemID - foreignkey - integer
sku - integer
QtyToList - integer

How can I tell to Django that ItemID is the ID field and it's also the PK ? (do I need to tell it also it's an integer field?)

What is the best way to model it in django.

Thanks in advance
Amit

Upvotes: 1

Views: 2291

Answers (2)

suselrd
suselrd

Reputation: 794

First, I recommend using the 'inspectdb' command, then you can modified the result as you want. Please visit the inspectdb documentation for more details on it.
The documentation is quite helpful.

Upvotes: 1

Brandon Taylor
Brandon Taylor

Reputation: 34553

class Item(models.Model):

    class Meta:
        db_table='Items'

    item_id = models.IntegerField(primary_key=True, db_column='ItemID')
    storage_location = models.CharField(max_length=100, db_column='StorageLocation')


class Inventory(models.Model):

    class Meta:
        db_table='Inventory'

    inventory_id = models.IntegerField(primary_key=True, db_column='InventoryID')
    item = models.ForeginKey('Item', db_column='ItemID')
    sku = models.IntegerField()
    qty_to_list = models.IntegerField(db_column='QtyToList')

Model Field Reference: https://docs.djangoproject.com/en/1.7/ref/models/fields/#db-column

Model Meta Reference: https://docs.djangoproject.com/en/1.7/ref/models/options/

Upvotes: 3

Related Questions