1
votes

This might be a simple Django question, but I hope I can get som advice. The following code in admin.py

class ExarbeteMomentInline(admin.TabularInline):
    model = ExarbeteMoment
    extra=0

class ExarbeteStudent(admin.TabularInline):
    model = ExarbeteStudent
    extra=0


class ExamensarbeteAdmin(admin.ModelAdmin):
     list_display = ('title', 'start_date', 'end_date', 'company')
     inlines = [ExarbeteMomentInline, ExarbeteStudent]

admin.site.register(Examensarbete,ExamensarbeteAdmin)

produces what I want, in the admin panel.

But I want a version of it outside the admin panel, so that regular users can enter data. How can I modify the code in a minimal way to get essentially the same page outside the admin region?

Thanks, in advance.

/ADDED/ My models are:

class Examensarbete(models.Model):
    title = models.CharField(max_length=10000,default='',blank=True)
    start_date = models.DateField('startdatum')
    end_date = models.DateField('slutdatum',blank=True,default='',null=True)
    company = models.CharField(max_length=200,default='',blank=True)
    comment = models.TextField(max_length=1000,default='',blank=True)
    kurskod = models.CharField(max_length=100,default='FMAM05')
    year = models.CharField(max_length=5,blank=True,default='',null=True)

 class ExarbeteMoment(models.Model):
    exarbete = models.ForeignKey(Examensarbete,on_delete=models.CASCADE)
    typ = models.CharField(max_length=100)
    person = models.ForeignKey(Personal,on_delete=models.SET_NULL,null=True)
    tim = models.FloatField(null=True)

class ExarbeteStudent(models.Model):
    exarbete = models.ForeignKey(Examensarbete,on_delete=models.CASCADE)
    first_name=models.CharField(max_length=100)
    last_name=models.CharField(max_length=100)
    pnr = models.CharField(max_length=13)
    program = models.CharField(max_length=5)
    kull = models.CharField(max_length=5)

where I have deleted str and Meta. I guess I should be able to solve the problem with the help below, but I can't still figure out how I get what I get in the admin panel, with Examensarbete above and then two subforms with ExarbeteMoment and ExarbeteStudent. And the 'add another item' feature.

Unfortunately I am new to Django, and find it particularly hard to work with forms and formsets. I am not quite sure why, because they should simplify things considerably.

1
Probably I am wrong, but I will not let you know that this is possible outside of the admin area. All the logic will need to be created by yourself, to get a list and add entries to the database - Roman Nozhenko

1 Answers

0
votes

I agree with the comment from Roman Kovalevsky - you will need to write the functions by yourself. Django however supports you with forms and formsets to do this job. As you did not post your models in the question i can only show you an example with some random named variables:

class ExarbeteStudent(forms.Form):
    description = forms.CharField(widget=AdminTextareaWidget)
    date1 = forms.DateField(widget=AdminDateWidget)
    date2 = forms.DateField(widget=AdminDateWidget)
    task_list = Task.objects.all()

then you create a formset for the inline models as following

ExarbeteStudentFormSet = forms.formset_factory(ExarbeteStudent,
                                            extra=1,
                                            max_num=60,
                                            can_delete=True,

Here you find some more informations about forms and formsets

https://docs.djangoproject.com/en/2.0/topics/forms/formsets/