I am developing a Django form to upload files. The form is based on a model(files). The model(files) has a foreign key field (repo_id). This repo_id is the primary key in the Repository model.
I want users to be able to choose which repo_id to put the file in. I need to insert a value to this field using a drop-down list in the form.
in the HTML template, the dropdown list is showing as "Repository object(repo_id)"
I have developed the two models (files and repository) in models.py, views.py, forms.py and HTML template following a tutorial. The form is working and it saves data in the database table as well.
I want to update the drop-down menu only to show indicator names in the repository model, not a repository object.
Here are my codes.
models.py
from django.db import models
# Create your models here.
class Repository(models.Model):
source = models.TextField(db_column='source', blank=True, null=True) # Field name made lowercase.
indicator = models.TextField(db_column='indicator', blank=True, null=True) # Field name made lowercase.
key_aspect = models.TextField(db_column='key_aspect', blank=True, null=True)
category = models.TextField(db_column = 'category')
table_id = models.TextField(db_column='table_id', blank=True, null=True)
data_returning = models.TextField(db_column='data_returning', blank=True, null=True)
data_source = models.TextField(db_column='data_source', blank=True, null=True)
value = models.TextField(db_column='value', blank=True, null=True) # Field name made lowercase.
year = models.TextField(db_column='year', blank=True, null=True) # Field name made lowercase.
location = models.TextField(db_column='location', blank=True, null=True) # Field name made lowercase.
class files(models.Model):
repo_id = models.ForeignKey(Repository, on_delete = models.CASCADE)
username = models.CharField(db_column='username',max_length = 45)
date = models.DateTimeField(auto_now_add=True, db_column = 'date')
file = models.FileField(upload_to='documents/', db_column = 'file')
username = models.CharField(db_column='username',max_length = 45)
def __unicode__(self):
return self.indicator_name
forms.py
class DocumentForm(forms.ModelForm):
class Meta:
model = files
fields = ('username', 'file', 'repo_id')
views.py
@login_required
def file_upload(request):
websiterepository = Repository
#indicator_queryset = websiterepository.objects.filter(category = 'P').values_list('id','indicator')
if request.method == 'POST':
documentform = DocumentForm(request.POST, request.FILES)
if documentform.is_valid():
documentform.save()
return redirect('file_upload')
else:
form = DocumentForm()
#documentform = DocumentForm(prefix = 'documentform')
#searchform = SearchForm(indicator_queryset = indicator_queryset, prefix = 'searchform')
#form = {'documentform':documentform, 'searchform':searchform}
#return render(request, 'form_file_upload.html', form )
return render(request, 'form_file_upload.html', {'form':form })
I know this is a lenthly post. I would be really grateful if you colud help me to figure this out.Thanks
Change the models to below code
class Repository(models.Model):
source = models.TextField(db_column='source', blank=True, null=True) # Field name made lowercase.
indicator = models.TextField(db_column='indicator', blank=True, null=True) # Field name made lowercase.
key_aspect = models.TextField(db_column='key_aspect', blank=True, null=True)
category = models.TextField(db_column = 'category')
table_id = models.TextField(db_column='table_id', blank=True, null=True)
data_returning = models.TextField(db_column='data_returning', blank=True, null=True)
data_source = models.TextField(db_column='data_source', blank=True, null=True)
value = models.TextField(db_column='value', blank=True, null=True) # Field name made lowercase.
year = models.TextField(db_column='year', blank=True, null=True) # Field name made lowercase.
location = models.TextField(db_column='location', blank=True, null=True) # Field name made lowercase.
def __str__(self):
return self.indicator
If you want model to return field value , you have to define a self function or else it will return the respective object .