0

I have a Organisation class with the following properties:

class Organisation(models.Model):
    user = models.OneToOneField(User, null=True)
    phoneNumber = models.CharField(max_length=12)
    description = models.CharField(max_length=1024)
    image = models.ImageField(upload_to='organisation_image')
    category = models.ForeignKey("Category")
    subscribedVolunteers = models.ManyToManyField(Volunteer)

    def __unicode__ (self):
        return self.name

I want to populate the database with this file:

def populate():
    organisations = [
        {"first_name":"Concrete Garden","description":description,
         "email":"[email protected]",
         "password":"cgarden",
         "username":"cgarden",
         "phoneNumber":"0141 237 9144",
         "category":categories_arr[0]}
        }

    organisations_arr = []
    for org in organisations:
        organisations_arr.append(add_organisation(org))

def add_organisation(org):
    print(org["username"])
    #ERROR HERE - when trying to create user object
    u = User.objects.create_user(username=org["username"], email=org["email"], first_name=org["first_name"],last_name=org["first_name"], password=org["password"])
    u.save()
    c = Organisation.objects.get_or_create(user=u,phoneNumber=org["phoneNumber"],description=org["description"],category=org["category"])[0]
    c.save()
    return c

if __name__ == '__main__':
    print("Starting population script...")
    populate()

Any comment or suggestion is greatly appreciated. Thank you.

1 Answer 1

1

You already have an entry for cgarden as a username for User in your database. This is what the error message means.

You can query your database withing the Django Shell (python manage.py shell). I suggest using a database exploration tool (maybe in your IDE) to make things easier.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.