Gettinfg Невозможно назначить, должен быть экземпляр. ошибка при назначении переменной модели - PullRequest
0 голосов
/ 28 мая 2020

Я не могу сохранить значение.

эта строка datafile.project = 41 дает мне:

Cannot assign "41": "Data.project" must be a "Project" instance.

Как решить эту проблему?

models.py:

class Project(models.Model):
    name = models.TextField()
    description = models.TextField(blank=True)
    creator = models.ForeignKey('Profile', on_delete=models.CASCADE)
    percentage_irr = models.FloatField(default=10.0, validators=[
                                       MinValueValidator(0.0), MaxValueValidator(100.0)])
    num_users_irr = models.IntegerField(default=2, validators=[MinValueValidator(2)])
    codebook_file = models.TextField(default='')
    batch_size = models.IntegerField(default=30)
    ''' Advanced options '''
    # the current options are 'random', 'least confident', 'entropy', and 'margin sampling'
    ACTIVE_L_CHOICES = [
        ("least confident", "By Uncertainty using Least Confident"),
        ("margin sampling", "By Uncertainty using the Margin"),
        ("entropy", "By Uncertainty using Entropy"),
        ("random", "Randomly (No Active Learning)")
    ]

    CLASSIFIER_CHOICES = [
        ("logistic regression", "Logistic Regression (default)"),
        ("svm", "Support Vector Machine (warning: slower for large datasets)"),
        ("random forest", "Random Forest"),
        ("gnb", "Gaussian Naive Bayes")
    ]

    learning_method = models.CharField(
        max_length=15, default='least confident', choices=ACTIVE_L_CHOICES)
    classifier = models.CharField(
        max_length=19, default="logistic regression", choices=CLASSIFIER_CHOICES, null=True)

    def get_absolute_url(self):
        return reverse('projects:project_detail', kwargs={'pk': self.pk}) #<--- Terminate Here

    def get_current_training_set(self):
        try:
            return self.trainingset_set.all().order_by('-set_number')[0]
        except IndexError:
            return None

    def admin_count(self):
        return self.projectpermissions_set.all().filter(permission='ADMIN').count()

    def coder_count(self):
        return self.projectpermissions_set.all().filter(permission='CODER').count()

    def labeled_data_count(self):
        return self.data_set.all().filter(datalabel__isnull=False).count()

    def has_model(self):
        if self.model_set.count() > 0:
            return True
        else:
            return False


class ProjectPermissions(models.Model):
    class Meta:
        unique_together = (('profile', 'project'))
    PERM_CHOICES = (
        ('ADMIN', 'Admin'),
        ('CODER', 'Coder'),
    )
    profile = models.ForeignKey('Profile',on_delete=models.CASCADE)
    project = models.ForeignKey('Project',on_delete=models.CASCADE)
    permission = models.CharField(max_length=5, choices=PERM_CHOICES)

class Data(models.Model):
    class Meta:
        unique_together = (('upload_id_hash', 'project'))
    project = models.ForeignKey('Project', on_delete=models.CASCADE)
    irr_ind = models.BooleanField(default=False)
    upload_id = models.CharField(max_length=128)
    upload_id_hash = models.CharField(max_length=128)

views.py:

def create_data_from_csv(df, pr):
    datafile = Data()
    datafile.project = 41
    datafile.save()

# s_obj = serializers.serialize("json", [datafile.project]) # convert object to json format
# print(s_obj)
...