0

My timesheet.html will have the variables of ID, name, startDate and endDate. I wish to display all these fields in a table in list_timesheet.html. But i have a problem of displaying the same ID and same name several times with different start and end date. Does anyone has any idea on what i should do on my models.py ? Right now i can only submit timesheet with the same ID and name ONCE, because of the foreign key problem,when i try to submit a timesheet with the SAME ID and name but DIFFERENT start and end date, it will show error saying "student ID and student Name already exist".

models.py

#consists of all the details in the timesheet
class Timesheet(models.Model):
    studentID = models.CharField("Student ID", max_length=8, primary_key=True, default="")
    studentName = models.CharField("Student Name", max_length=500, default="")
    startDate = models.DateField("Start Date", max_length=8)
    endDate = models.DateField("End Date", max_length=8)

    def __str__(self):
        return self.studentID 

#consists of all the details of the timesheet under 'View Timesheets'        
class LTimesheet(models.Model):
    timesheet = models.OneToManyField(Timesheet, on_delete=models.CASCADE, primary_key=True)
    status = models.CharField("Status", max_length=100)
  • Describe your problem. It's hard to understand what exactly you need help with. – Håken Lid Jun 29 '17 at 18:15
  • I think you're looking for `ForeignKey` not `OneToManyField`. https://stackoverflow.com/questions/6928692/how-to-express-a-one-to-many-relationship-in-django – Brobin Jun 29 '17 at 18:39
  • @HåkenLid i have described my problem –  Jun 30 '17 at 04:19
  • @Brobin have tried ForeignKey before, but it doesn't allow me to have several submission on the same ID and name with different start and end date –  Jun 30 '17 at 04:20

1 Answers1

0

when i try to submit a timesheet with the SAME ID and name but DIFFERENT start and end date, it will show error saying "student ID and student Name already exist".

Yes, Django raises error, not because of ForeignKey, but because of the field studentID is set to primary_key=True. Also, remove the primary_key=True from the ForeignKey in the LTimesheet model also.

If you want to save multiple Timesheet objects with same ID and name, then I would suggest models to be like this somewhat,

class Timesheet(models.Model):
    studentID = models.CharField("Student ID", max_length=8, default="")
    studentName = models.CharField("Student Name", max_length=500, default="", null=True, blank=True)
    startDate = models.DateField("Start Date", max_length=8, null=True, blank=True)
    endDate = models.DateField("End Date", max_length=8, null=True, blank=True)

    def __str__(self):
        return self.studentID 

class LTimesheet(models.Model):
    timesheet = models.ForeignKey(Timesheet, on_delete=models.CASCADE)
    status = models.CharField("Status", max_length=100)
zaidfazil
  • 9,017
  • 2
  • 24
  • 47
  • if i remove the primary_key = true, will have error saying "trying to add non-nullable field "id" to ltimesheet without a default" –  Jun 30 '17 at 07:05
  • Did you migrate after the change?? – zaidfazil Jun 30 '17 at 07:09
  • have to makemigrations first then only can migrate i thought ? when i run make migrations will show the error that i mentioned –  Jun 30 '17 at 07:13
  • The change in the primary key affects all data in your database, it would be much better if you just took a backup for your database and migrations folder. – zaidfazil Jun 30 '17 at 07:15
  • Then delete the migrations and drop the table and recreate the database. Then only the problem would be solved – zaidfazil Jun 30 '17 at 07:16
  • I have tried "python manage.py migrate --fake zero" and then "python manage.py makemigrations" but still getting the same error –  Jun 30 '17 at 07:25
  • I'm afraid then you would have to delete all the tables and re-create from scratch. – zaidfazil Jun 30 '17 at 08:38
  • alright. Thank you so much. By the way the code that you given, will work for having the same ID and name appear several times with the different dates being stored into the database right ? –  Jun 30 '17 at 08:41
  • In the code I've given, I have removed the primary_key constraint from the field. Then, it should create multiple rows with same ID, and name, unless you haven't added unique=True in the field. It would definitely work. – zaidfazil Jun 30 '17 at 08:44
  • are u saying add "unique=True" in studentID in timesheet view and in imesheet in ltimehseet view ? –  Jun 30 '17 at 09:17
  • No, I'm saying don't add it. If you add it, the error would persist. – zaidfazil Jun 30 '17 at 09:18
  • i havent added anyting in ur given code. It is exactly the same. I think i will delete all the migrations then do it over again. Do u have any idea on how do i drop a table and recreate the database ? –  Jun 30 '17 at 09:22
  • Which database are you using? – zaidfazil Jun 30 '17 at 09:23
  • thats wat u said in the previous comment i thought ? "Then delete the migrations and drop the table and recreate the database. Then only the problem would be solved " –  Jun 30 '17 at 09:28
  • Yes, I know. Are you using SQLite3 or PostgreSQL?? – zaidfazil Jun 30 '17 at 09:29
  • i am using SQLite3 –  Jun 30 '17 at 09:29
  • Then, run `rm -r /migrations` , then `rm -rf db.sqlite3`. Then makemigrations and migrate again. – zaidfazil Jun 30 '17 at 09:31
  • seems like rm is not recognized as an internal or external command.do i have to type python manage.py in front ? –  Jun 30 '17 at 09:41
  • Are you not using ubuntu? No need for command, you could just delete the migrations folder in your app directory and delete the file `db.sqlite3` in your project directory. – zaidfazil Jun 30 '17 at 09:44