Home > Mobile >  Django: Multiple inputs with foreign key
Django: Multiple inputs with foreign key

Time:10-17

I wanted to add more users for a shift. But it's a foreign key. How to do I make it? It does add, but if I choose more than one user, it shows this.

Field 'id' expected a number but got ['1', '2']

Should I convert it to string? I don't know how to do it. Below is my code.

models.py

class User(models.Model):
    user_name = models.CharField(max_length=32, unique=True)
    pass_word = models.CharField(max_length=150,)
    email = models.EmailField(blank=True, unique=True)
    phone = models.CharField(max_length=32, unique=True)
    is_active = models.BooleanField(default=True,)

class Attendance(models.Model):
    RosteringUserDate = models.ForeignKey(RosteringUserDate, on_delete=models.CASCADE, null=True)
    date = models.DateField()
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    begin_time = models.TimeField(default="")
    end_time = models.TimeField(default="")
    work_time = models.CharField(max_length=64, default='')

views.py

def shift_add(request):
    queryset = User.objects.all()
    if request.method == 'GET':
        return render(request, 'attendance/shift_add.html', {'queryset': queryset})
    if request.method == "POST":   
        Attendance.objects.create(
            user_id = request.POST.getlist('user_name',[]),
            date = request.POST.get('date'),
            RosteringUserDate_id = request.POST.get('RosteringUserDate_id'),
            begin_time = request.POST.get('begin_time'),
            end_time = request.POST.get('end_time'),
            work_time = request.POST.get('work_time'),
        )
        return redirect('/user/attendance/')

forms.py

<form method="post"  action="/user/attendance_administrators/shift/add/">
    <div >
         <input type="text" name="name"  required>
    </div>
    <div >
         <input type="date" name="date"  required>
    </div>
    <div >
         <input type="text" name="RosteringUserDate_id"  required>
    </div>
    <div >
         <input type="time" name="begin_time"  required>
    </div>
    <div >
         <input type="time" name="end_time"  required>
    </div>
    <div >
         <input type="text" name="work_time"  required>
    </div>
    <div >
         <select name="user_name"  required multiple>
                 {% for query in queryset%}
                 {% if query in queryset.all %}
                 <option selected value="{{ query.id }}"> {{ query.user_name }} </option>
                 {% else %}
                  <option value="{{ query.id }}"> {{ query.user_name }} </option>
                 {% endif %}
                 {% endfor %}
         </select>
    </div>
    <div align="center">
          <input type="submit" value="Submit"  >
          <input type="reset" value="Reset"  >
    </div>
</form>

CodePudding user response:

When you use ForeignKey that mean you can add multiple records in table with same ForeignKey id field but only one in same records, so one of the solutions you can use bulk_create

CodePudding user response:

The right way for these type of problem is using the ManyToMany field.

Example:

user = models.ManyToManyField(User)

This demonstration will help you to get easy understanding.

  • Related