eagle06
eagle06

Reputation: 3

Django Models relations

Below is my Django models code

    from django.db import models


class BookUser(models.Model):
    email= models.CharField(max_length=254,primary_key=True)          #mail address key
    name = models.CharField(max_length=254)                  #max 64 char (lower case?)
    contact= models.CharField(max_length=12)
    imei = models.CharField(max_length=16)                #imei number
    address= models.TextField()            #list of address ids
    booksInShelf:[]        #list of user book's unique ids
    booksUnderCirculation:[]     #list of user book's unique ids


    class Meta:
        ordering = ('email',)

class Book(models.Model):
    isbn = models.CharField(max_length=13)
    title=models.CharField(max_length=500)
    description =models.TextField()
    author = models.CharField(max_length=200)
    userRating = models.CharField(max_length=1)
    users =            #list of user ids hold this book in shelf

class UserBook(models.Model):
    #id:                    generated by django
    bookId:               #id of parent book
    rent= models.BooleanField(default=False)           #boolean is ready to rent
    sell= models.BooleanField(default=False)                    #boolean is ready to sell
    price =models.FloatField()              #selling price
    rentBase=models.FloatField()          #base price of rent
    rentPeriod=models.IntegerField()             #days after which extra rent would apply
    dateModified =models.DateTimeField(auto_now=True)             #track date it came into shelf
    dateAdded = models.DateTimeField(auto_now_add=True)

Here BookUser is the actual user who has some books in two categories i.e booksinShelf and bookUnderCirculation

class Book is central repository of all books, I need to define a one to many relation to BookUser.What is the easy way to do this?

User Book is specific to BookUser and it should be uniquely pointing to Class Book , So its many to one relation to Book Class.

I am confused on how to handle ids of UserBook and Book? Also how to store the list of ids of UserBooks in class BookUser??

Upvotes: 0

Views: 204

Answers (2)

Chitrank Dixit
Chitrank Dixit

Reputation: 4051

After looking at the Models and explanation provided below the Book model the users field should have ForeignKey relationship with the BookUser model.

so Book model should look like

class Book(models.Model):
    isbn = models.CharField(max_length=13)
    title=models.CharField(max_length=500)
    description =models.TextField()
    author = models.CharField(max_length=200)
    userRating = models.CharField(max_length=1)
    users = models.ForeignKey(BookUser, null=True, blank=True)           

if you are using Postgresql and if you just need the pk list of booksInShelf and booksUnderCirculation then your BookUser model should look like

class BookUser(models.Model):
    email= models.CharField(max_length=254,primary_key=True)                 
    name = models.CharField(max_length=254)                  
    contact= models.CharField(max_length=12)
    imei = models.CharField(max_length=16)                
    address= models.TextField()            
    booksInShelf = models.ArrayField(models.IntegerField())        
    booksUnderCirculation = models.ArrayField(models.IntegerField())        

and if you wish to have the full information of booksInShelf and booksUnderCirculation (not just the pk but other information related to the book as well), then you need to define it as ManyToMany relation.

class BookUser(models.Model):
        email= models.CharField(max_length=254,primary_key=True)                 
        name = models.CharField(max_length=254)                  
        contact= models.CharField(max_length=12)
        imei = models.CharField(max_length=16)                
        address= models.TextField()            
        booksInShelf = models.ManyToMany(UserBook)
        booksUnderCirculation = models.ManyToMany(UserBook)

also rather than creating two ManyToMany fields in the BookUser model you can have two flags in your UserBook model called is_in_shelf and is_under_circulation. These fields would be BooleanField, you can check more about the model fields in Django Documentation here: https://docs.djangoproject.com/en/1.10/topics/db/models/#fields

Upvotes: 1

Bestasttung
Bestasttung

Reputation: 2458

This should do what you want :

class UserBook(models.Model):
    bookId = models.ForeignKey('Book')

Here a UserBook has a reference to a Book item, and severals users can have the same book, but it's still a unique reference in you table Book.

Hope it helps

Upvotes: 0

Related Questions