我正在尝试构建多项选择测验Django应用。我有一个名为Answer
的模型,还有一个名为Question
的模型。
以下是Answer
的内容:
class Answer(models.Model):
text = models.CharField(max_length=255)
这是Question
:
class Question(models.Model):
text = models.CharField(max_length=255)
correct_answer = models.ForeignKey('Answer', on_delete=models.CASCADE, related_name='correct_answers')
other_answers = models.ManyToManyField('Answer')
我想将other_answers
中django-admin
的选择数量限制为仅3个答案。该怎么做?
注意:
django-forms
,我只会为移动应用构建API。答案 0 :(得分:0)
如果您想将其限制为3个具体答案,我想您可以使用limit_choices_to
如果您想将其限制为最多3个,则应使用django model validation
答案 1 :(得分:0)
感谢Geoff Walmsley的回答启发了我正确的答案。
这是解决方案:
admin.py :
if (intent.getAction().equals(NfcAdapter.ACTION_NDEF_DISCOVERED)) {
Toast.makeText(getApplicationContext(), "Ndefdiscovered", Toast.LENGTH_SHORT).show();
} else if (intent.getAction().equals(NfcAdapter.ACTION_TAG_DISCOVERED)) {
mTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
Toast.makeText(getApplicationContext(), "Smartcard detected", Toast.LENGTH_SHORT).show();
StartTime = SystemClock.uptimeMillis();
handler.postDelayed(runnable, 0);
NdefMessage[] messages = getNdefMessage(intent);
if (messages == null) {
Toast.makeText(getApplicationContext(), "Data di dalam kartu kosong", Toast.LENGTH_SHORT).show();
return;
}else{
TimeBuff += MillisecondTime;
handler.removeCallbacks(runnable);
}
byte[] payload = messages[0].getRecords()[0].getPayload();
userData = new String(payload);
TextView tvNFCContent = findViewById(R.id.tv_nfc_content);
tvNFCContent.setText("NFC Content: " + userData);
} else {
Toast.makeText(getApplicationContext(), "Undefined smartcard", Toast.LENGTH_SHORT).show();
}