如何使用django-autocomplete-light和django-hacker在django管理中创建一个链式下拉菜单?

8zzbczxx  于 2023-01-27  发布在  Go
关注(0)|答案(1)|浏览(85)

我有三种模式:大学课程和调查。
我的查询模型有一个指向"course"的外键。而course有一个指向大学的外键。我只想在添加查询时显示与该大学相关的课程。我尝试了django-smart-select但失败了。我尝试了This answer但我不理解其逻辑,未能在我的项目中实现。
这是我的www.example.com文件models.py file

class university(models.Model):

    univ_name = models.CharField(max_length=100)
    country = CountryField(blank=True, null=True)
    univ_desc = models.CharField(max_length=1000)
    univ_logo = models.ImageField(upload_to="media")
    univ_phone = models.CharField(max_length=10, blank=True)
    univ_email = models.EmailField(max_length=254, blank=True)
    univ_website = models.URLField(blank=True)
    assigned_users = models.ForeignKey(get_user_model(), on_delete=models.CASCADE, default="")

    def __str__(self):
        return self.univ_name

    class Meta:
        verbose_name_plural = "Universities"

class Course(models.Model):

    university = models.ForeignKey(university, on_delete=models.CASCADE)
    course_name = models.CharField(max_length=100)
    course_levels = models.ForeignKey(course_levels, on_delete=models.CASCADE)
    intake = models.ForeignKey(intake, on_delete=models.CASCADE)
    documents_required = models.ForeignKey(documents_required, on_delete=models.CASCADE)
    course_requirements = models.ForeignKey(course_requirements, on_delete=models.CASCADE)
    Active = models.BooleanField()

    def __str__(self):
        return self.course_name


class enquiry(models.Model):

    student_name = models.CharField(max_length=100)
    student_phone = models.CharField(max_length=10)
    student_email = models.EmailField()
    student_address = models.TextField()
    current_education = models.ForeignKey(current_education, on_delete=models.CASCADE)
    country_interested = CountryField(blank=True, null=True)
    university_interested = models.ForeignKey(university, on_delete=models.CASCADE)
    course_interested = models.ForeignKey(Course, on_delete=models.CASCADE, limit_choices_to={'Active':True})
    level_applying_for = models.ForeignKey(course_levels, on_delete=models.CASCADE)
    intake_interested = models.ForeignKey(intake, on_delete=models.CASCADE)
    assigned_users = models.ForeignKey(User, on_delete=models.CASCADE, default="", limit_choices_to={"is_active": True})
    enquiry_status = models.ForeignKey(enquiry_status, on_delete=models.CASCADE, default="")

    course_interested= ChainedForeignKey(Course,chained_field= 'university_interested',chained_model_field= 'university',show_all= False,auto_choose= True,sort=True,limit_choices_to = {"Active": True},)

我想显示与那所大学有关的课程-感兴趣的领域。需要帮助。
我试过使用django-smart-select但是没有实现,我不知道jquery和ajax,所以在我的项目中使用是不可能的。

o75abkj4

o75abkj41#

我使用了django-select 2,并且能够解决这个问题。

相关问题