How to make a form that can add an unlimited amount of elements

I’m trying to make my schools web-site for students. In this site i want to allow them create project page. In project page I want to allow them to upload unlimited number of files that can be dowloaded by other users. Image from template

How I can do that

from django.db import models
from django.contrib.auth.models import User
import uuid
from phonenumber_field.modelfields import PhoneNumberField

class News(models.Model):
	user = models.ForeignKey(User, on_delete=models.CASCADE, editable = False)
	name = models.CharField(max_length=50)
	description = models.TextField(null=True, blank=True)
	images = models.ImageField(upload_to='media/', default=None, null=True, blank=True)
	face_image = models.ImageField()
	created = models.DateTimeField(auto_now_add=True)
	text = models.TextField()
	id = models.UUIDField( 
         primary_key = True, 
         default = uuid.uuid4, 
         editable = False)
	class Meta:
		ordering = ["-created"]
class profile(models.Model):
	user = models.ForeignKey(User, on_delete=models.CASCADE,)
	profile_image = models.ImageField(upload_to='profileimages/', default=None, null=True, blank=True)
	bio = models.CharField(null=True, blank=True, max_length=130)
	Date_of_birth = models.DateField(null=True, blank=True)
	phone_number = PhoneNumberField(null=True, blank=True, region="UZ")
	job = models.CharField(null=True, blank= True, max_length=50)
	id = models.UUIDField( 
         primary_key = True, 
         default = uuid.uuid4, 
         editable = False)
	about = models.TextField(null=True, blank=True)
# Create your models here.
class Skills(models.Model):
	skills = models.CharField(max_length=30)
	user = models.ForeignKey(User, on_delete=models.CASCADE)
class Project(models.Model):
	host = models.ForeignKey(User, on_delete=models.CASCADE)
	date = models.DateField()
	category = models.CharField(null=True, blank=True, max_length=20)
	cost = models.CharField(null=True, blank=True, default="0.0$", max_length=10)
	profit = models.CharField(null=True, blank=True, default="Non-profit", max_length = 11)
	time = models.CharField(null=True, blank=True, max_length=10)
	Website = models.CharField(null=True, blank=True, default=None)
	name = models.CharField(max_length=30)
	description = models.TextField()
	mission = models.CharField(max_length=100)
	vision = models.CharField(max_length=100)
	result = models.CharField(max_length=100)
	id = models.UUIDField( 
         primary_key = True, 
         default = uuid.uuid4, 
         editable = False)
class projectfiles(models.Model):
	file = models.FileField(upload_to ='projectfiles/')
	field_name = 'id'
	obj = Project.objects.first()
	field_value = getattr(obj, field_name)

As you can see I tried to create another model with file field and with project ID and if IDs match template should show the file but that didn’t work(gave error: django.db.utils.OperationalError: no such table: base_project, but i think even without error it wouldn’t work because of ). I used same tactics in Skills model to show users skills in profile page, but with user nickname which is unique. Image of profile page

<h4 class="card-title mb-4">My Skill</h4>
                    <div class="d-flex gap-2 flex-wrap">
                        {% for i in skill %}
                        {% if i.user == user.user%}
                        <span class="badge badge-soft-secondary p-2">{{i.skills}}</span>
                        {% endif %}
                        {% endfor %}
                    </div>

I can’t use nickname to projects because person can have multiple projects. I need something to identify project and use this to do same method as in Skills or another method that can solve my problem

Hello

I think you should check you model well and you will notice some issues with the way you nomalize your Project and projectfiles tables.

Instead it maybe better if it look something like

class Project(models.Model):
    host = models.ForeignKey(User, on_delete=models.CASCADE)
    date = models.DateField()
    name = models.CharField(max_length=30)
    ...



class ProjectFiles(models.Model):
    project = models.ForeignKey(Project, on_delete=models.CASCADE)
    file = models.FileField(upload_to='projectfiles/')
    ....

The problem with your file model

class projectfiles(models.Model):
	file = models.FileField(upload_to ='projectfiles/')
	field_name = 'id'
	obj = Project.objects.first()
	field_value = getattr(obj, field_name)

is this field obj = Project.objects.first() which refers object and has not relatioships with project in database level.

Thank you for solution!!!

1 Like