0

I want to be able to allow my user to add or remove foreign keys on one of my models. The business logic behind this is that there are many unique products that relate to one "product bundle". I want users to create their own product bundles by creating a new bundle object, then adding or removing products as they see fit.

    from django.db import models


# Create your models here.

class Product(models.Model):
    product_name = models.CharField(max_length=128)

    def __str__(self):
        return self.product_name


class ProductBundle(models.Model):
    bundle_name = models.CharField(max_length=128)
    product_dependencies = models.ForeignKey(Product, related_name="add_product",
                                             on_delete=models.CASCADE)

    def __str__(self):
        return self.bundle_name

enter image description here

Currently its a list with all of the products, which is not the functionality I'm trying to achieve. A lot of posts talk about adding the related_name field to achieve this but for whatever reason it's not working for me

Justin S
  • 1,409
  • 4
  • 22
  • 38
  • Does this answer your question? [filter foreignkey field in django admin](https://stackoverflow.com/questions/10179129/filter-foreignkey-field-in-django-admin) – JPG Jan 21 '20 at 04:11
  • doesn't look like the answer im chasing. That post looks more about filtering, I want to be able to actually create models with references to fk in other models at the start – Justin S Jan 21 '20 at 04:21
  • I might be totally of track but when reading you explanation I understood that a ```ProductBundel``` could contain many ```Products```. From your code I see that one Product can have a relationship with many bundles but not the other way round. Is this what you are trying to achieve? – Chris Jan 21 '20 at 07:17
  • good point. I'm not exactly sure. It makes sense that a Product can be related to many bundles. It also makes sense that a bundle can be related to many products but I don't think I need to have this if I can build the relationship just with products being related to many bundles – Justin S Jan 21 '20 at 20:47

1 Answers1

0

Answer:

So long story short I should have been using a many to many field. I also was loading the default django admin view which displays all references stored(not added)

in admin.py

from django.contrib import admin
from .models import Product, ProductDependency

# Register your models here.
admin.site.register(Product)


class ProductDependencyAdmin(admin.ModelAdmin):
    filter_horizontal = ('product_dependencies',)


admin.site.register(ProductDependency, ProductDependencyAdmin)

and in models.py

from django.db import models import uuid

# Create your models here.

class Product(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    product_name = models.CharField(max_length=128)

    def __str__(self):
        return self.product_name


class ProductDependency(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    product = models.ForeignKey(Product, on_delete=models.DO_NOTHING, related_name='product')
    product_dependencies = models.ManyToManyField('Product')

    def __str__(self):
        return self.product.__str__()
Justin S
  • 1,409
  • 4
  • 22
  • 38