CodeSpent
CodeSpent

Reputation: 1914

Django ManyToOne?

I have an inventory management application, and inside is an Item() class that represents the product. All of the devices will be serialized so there will be a class later that represents each device called OnHand().

Item Model

class Item(models.Model):
    name = models.CharField(max_length=100)
    description = models.CharField(max_length=100)
    manufacturer = models.ForeignKey('Manufacturer', blank=True, null=True, on_delete=models.SET_NULL)
    introduction = models.DateField(auto_now=True)
    quanity = models.IntegerField(default=0)
    is_retired = models.BooleanField(default=False)
    tags = models.ManyToManyField(Tag)

    def __str__(self):
        return self.name

    def add(self):
        pass

    def remove(self):
        pass

    def retire(self): # Rex came up with this, roll credits.
        pass 

    def count(self):
        pass

I want an attribute for on_hand that has a ManyToOne type relationship.

OnHand Model Concept

class OnHand(models.Model):
    name = models.CharFiled(max_length=100)
    serial = models.CharField(max_length=80)
    asset = models.CharField(max_length=20)

    def __str__(self):
        return self.serial

When creating a new OnHand object, I'd like to associate it with the Item. How would I go about doing this?

Structural Example

Current Test Code

def make_macbook():
    """ Create a Manufacturer for Apple """
    apple = Manufacturer(name='Apple', rep_name='Jason Wilburn', support_number='1-800-Apple')
    apple.save()
    print('Manufacturer name: {}'.format(apple))

    """ Create a Category for Laptops """
    laptop = Category(name='Laptops')
    laptop.save()
    print('Category name: {}'.format(laptop))

    """ Create a Tag for Mac """
    mac = Tag(title='mac')
    mac.save()
    print('Tag name: {}'.format(mac))

    """ Create a MacBook Item """
    macbook = Item(name='Macbook Pro', description='15.5" Macbook Pro, 2018 Model', manufacturer=apple)
    macbook.save()
    print('Item name: {}'.format(macbook))

    """ Create a MacBook OnHand item """
    newMac = OnHand(name='MacBook Pro 15.5"', serial='SC02XP0NRJGH5', asset='DEPSC02XP0NRJGH5', product=macbook)
    newMac.save()

    """ Add to Item's OnHand """

Now I'm mainly just stuck on adding to the Item() OnHand to create the relationship.

Upvotes: 0

Views: 380

Answers (1)

devdob
devdob

Reputation: 1504

You can add a foreign key from your OnHand model to the Item model to achieve this. When you create an OnHand object, you just tie it to the required Item object.

class OnHand(models.Model):
   name = models.CharFiled(max_length=100)
   serial = models.CharField(max_length=80)
   asset = models.CharField(max_length=20)
   item = models.ForeignKey(Item)

   def __str__(self):
       return self.serial

Then in your view, when creating an OnHand object,

...
""" Create a MacBook Item """
macbook = Item(name='Macbook Pro', description='15.5" Macbook Pro, 2018 Model', manufacturer=apple)
macbook.save()
print('Item name: {}'.format(macbook))

""" Create a MacBook OnHand item """
newMac = OnHand(name='MacBook Pro 15.5"', serial='SC02XP0NRJGH5', asset='DEPSC02XP0NRJGH5', product=macbook, item=macbook)
newMac.save()
...

Hope this helps!

Upvotes: 1

Related Questions