我有一个django表单,用于向我的数据库添加新数据&引导选择框
如何使用选择框的项填充表单?
html:
class UnitSelectForm(forms.ModelForm):
class Meta:
model = lesson_student
fields = ('lesson_code','student_code')
forms.py:
class student(models.Model):
First_Name = models.CharField(max_length=100,null=True)
Last_Name = models.CharField(max_length=100,null=True)
STNO = models.CharField(max_length=10,null=True)
class lesson(models.Model):
Name = models.CharField(max_length=100,null=True)
Code = models.CharField(max_length=100,null=True)
class lesson_student(models.Model):
lesson_code = models.CharField(max_length=100)
student_code = models.CharField(max_length=100)
models.py:
//The Main Activity
public class FullscreenActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
}
Context context=this;
Alarm alarm = new Alarm();
public void onDummyButton(View view) {
alarm.SetAlarm(this);
}
}
//The Alarm Receiver
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
import java.util.Calendar;
public class Alarm extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
//Log.d("Alarm", "onReceive");
// Put here YOUR code.
// Start the MainActivity
Intent i = new Intent(context, FullscreenActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // For example
}
public void SetAlarm(Context context)
{
AlarmManager am =( AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, Alarm.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis() + 1000 * 15);
//calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY));
//calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE)+1);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * 10, pi); // Millisec * Second * Minute
Toast.makeText(context, "Alarm scheduled for: " + calendar.get(Calendar.HOUR_OF_DAY)+':'+calendar.get(Calendar.MINUTE)+':'+calendar.get(Calendar.SECOND), Toast.LENGTH_LONG).show();
}
public void CancelAlarm(Context context)
{
Intent intent = new Intent(context, Alarm.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
}
当我在选择框中选择一个项目时,每个表单的字段按该项填充
例如,当我在学生的选择框中选择Student1时...第一个学生表单的字段会自动填充 {{student.STNO}}
有可能???
答案 0 :(得分:1)
您可以将其与一些javascript代码一起破解以处理选择,将学生和课程详细信息从下拉框复制到表单中,但为什么不只是首先修复模型
class lesson_student(models.Model):
lesson = models.ForeignKey(Lesson)
student = models.ForeignKey(Student)
现在,您不仅拥有有关其代码的详细信息......还可以使用完整模型。由于ModelForm
会照顾您的表单,因此您在表单方面也不会做任何其他事情。
我的列表中存在问题.....在学生或课程列表中我有“学生对象”& “课程对象”而不是名称
那是因为您没有为模型提供string representation。
即
class student(models.Model):
First_Name = models.CharField(max_length=100,null=True)
Last_Name = models.CharField(max_length=100,null=True)
STNO = models.CharField(max_length=10,null=True)
def __str__(self):
return "{0} {1}".format(self.First_Name, self.Last_Name)