skip to Main Content

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 class="form-group">
         <input type="text" name="name" class="form-control" required>
    </div>
    <div class="form-group">
         <input type="date" name="date" class="form-control" required>
    </div>
    <div class="form-group">
         <input type="text" name="RosteringUserDate_id" class="form-control" required>
    </div>
    <div class="form-group">
         <input type="time" name="begin_time" class="form-control" required>
    </div>
    <div class="form-group">
         <input type="time" name="end_time" class="form-control" required>
    </div>
    <div class="form-group">
         <input type="text" name="work_time" class="form-control" required>
    </div>
    <div class="form-group">
         <select name="user_name" class="form-control" 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" class="btn btn-primary" >
          <input type="reset" value="Reset" class="btn btn-primary" >
    </div>
</form>

2

Answers


  1. 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

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search